1
Answer

Filling a DataGridView containing a ComboBox

Akhil NP

Akhil NP

16y
6k
1

Hi,

I am creating a windows application in which i have got a DataGridView containing some comboboxes and checkboxes. Is there a way to fill the DataGridView from the database. Now i am filling the datagrid cell by cell seperately. Also there exist problem while updating the database. I am giving my code with this. Will anyone help me out of this.

1 public void LoadData(int tabcnt)  
2 {  
3 string module = this.tabControl1.SelectedTab.Text.Trim();  
4  
5 string connString;  
6  
7 string query = "";  
8  
9 connString = ConfigurationSettings.AppSettings.Get("ProjDB").ToString().Trim();  
10  
11 if (module != "")  
12 {  
13 query = "SELECT * FROM Module_Requirements where Module='" + module + "'";  
14 }  
15 else 
16 {  
17 MessageBox.Show("There Is No Modules", "No Modules", MessageBoxButtons.OK, MessageBoxIcon.Information);  
18 }  
19  
20 dAdapter = new MySqlDataAdapter(query, connString);  
21  
22 MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(dAdapter);  
23  
24 DataSet ds = new DataSet();  
25  
26 ds.Clear();  
27  
28 try 
29 {  
30 dAdapter.Fill(ds);  
31 BindingSource bSource = new BindingSource();  
32 }  
33 catch (Exception ex)  
34 {  
35 MessageBox.Show(ex.Message);  
36 }  
37  
38 //ucMod[tabcnt].gridLineDataGridView1.DataSource = ds; //this code is not working  
39  
40 int iRowCnt = ds.Tables[0].Rows.Count;  
41  
42 ucMod[tabcnt].gridLineDataGridView1.Rows.Clear();  
43  
44 for (int ctrRow = 0; ctrRow < iRowCnt; ctrRow++)  
45 {   
46 ucMod[tabcnt].gridLineDataGridView1.Rows.Add();  
47 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[1].Value = ucMod[tabcnt].Column2.Items[ucMod[tabcnt].Column2.Items.IndexOf(ds.Tables[0].Rows[ctrRow][1].ToString().Trim())];   
48 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[2].Value = ds.Tables[0].Rows[ctrRow][0].ToString().Trim();  
49 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[3].Value = ds.Tables[0].Rows[ctrRow][2].ToString().Trim();  
50 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[4].Value = ds.Tables[0].Rows[ctrRow][3].ToString().Trim();  
51 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[5].Value = ds.Tables[0].Rows[ctrRow][4].ToString().Trim();  
52 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[6].Value = ds.Tables[0].Rows[ctrRow][5].ToString().Trim();  
53 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[7].Value = ds.Tables[0].Rows[ctrRow][6].ToString().Trim();  
54 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[8].Value = ucMod[tabcnt].Column9.Items[ucMod[tabcnt].Column9.Items.IndexOf(ds.Tables[0].Rows[ctrRow][7])];  
55 ucMod[tabcnt].gridLineDataGridView1.Rows[ctrRow].Cells[9].Value = ucMod[tabcnt].Column10.Items[ucMod[tabcnt].Column10.Items.IndexOf(ds.Tables[0].Rows[ctrRow][8].ToString().Trim())];   
56 }  
57 }

 

Regards

Akhil.N.P

 

Answers (1)