Anyone can please tell me what I m doing wrong here.
I have a datagridview that is automatically filled at runtime using a dataset. Also In this datagridview each column have a dataGridViewCheckBoxColumn.
Now what I am trying to do in the code below is to programmatically create and fill a new datatable "dt". And then loop thru this "dt" and if the row exists in "dt" then set the dataGridViewCheckBoxColumn value to true in my datagridview.
Thank You
1: try
2: {
3: catalog_id = (string)(comboBox6.SelectedValue);
4: major_id = (string)(comboBox5.SelectedValue);
5: coursesBox.Visible = true;
6: categorie_id = dataGridView4.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();
7: DataTable dt = new DataTable();
8: // Fill the Set with the data
9: using (SqlConnection conn = new SqlConnection(connString))
10: {
11: StringBuilder query = new StringBuilder();
12: query.Append("SELECT a.course_id AS Courses, b.course_title AS Title, b.course_credit AS Credit, c.categories_desc AS Categories FROM degree_requirements a, Courses b, categories c, degree d WHERE a.course_id = b.course_id AND a.categories_id = c.categories_id AND a.degree_id = d.degree_id AND a.catalog_id = @ctl AND a.degree_id = @maj AND c.categories_desc = @cat");
13: SqlCommand cmd = new SqlCommand();
14: cmd.Connection = conn;
15: cmd.CommandText = query.ToString();
16: cmd.CommandType = CommandType.Text;
17: cmd.Parameters.AddWithValue("ctl", catalog_id);
18: cmd.Parameters.AddWithValue("maj", major_id);
19: cmd.Parameters.AddWithValue("cat", categorie_id);
20: //Passing the query and connection String
21: using (SqlDataAdapter da = new SqlDataAdapter(cmd))
22: {
23: da.Fill(dt);
24: }
25: }
26: // TO DO: Itierate thru the dataTable and If the row exist select the check box
27: //Loop over the Rows
28: foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
29: {
30: foreach(DataRow dr in dt.Rows)
31: {
32: //Loop over the items
33: foreach (var item in dr.ItemArray)
34: {
35: dataGridRow.Cells["dataGridViewCheckBoxColumn1"].Value = true;
36: }
37: }
38: }
39: groupBox3.Visible = true;
40: }
41: catch (System.Exception ex)
42: {
43: System.Windows.Forms.MessageBox.Show(ex.Message);
44: }