开发者

C# DataGridView combobox add data programmatically

开发者 https://www.devze.com 2023-04-03 00:54 出处:网络
I have an application with a DataGridView. One of the columns is of the type Combobox. I want to add the items for this combobox programmatically. Here is the code I use for that:

I have an application with a DataGridView. One of the columns is of the type Combobox. I want to add the items for this combobox programmatically. Here is the code I use for that:

this.dsStatussen = this.statussenMan.getAllStatussen();
        DataGridViewComboBoxColumn cd = (DataGr开发者_如何学PythonidViewComboBoxColumn)this.dgvEenheden.Columns[3];
        cd.DataSource = dsStatussen;
        cd.DisplayMember = "statussen";
        cd.DataPropertyName = "sid";
        cd.ValueMember = "status";

Then when I try to add a row I get the following error: "There is no field with the name status". I transelated the error to English because I have a Dutch error. Here is the code I use for adding the rows:

Eenheden eenhedenMan = new Eenheden(objEvenement.eid);
        DataSet EenhedenData = eenhedenMan.getAllEenheden();

        foreach (DataRow dr in EenhedenData.Tables[0].Rows)
        {
            dgvEenheden.Rows.Add(
                                    dr[0].ToString(), 
                                    dr[1].ToString(), 
                                    dr[2].ToString(), 
                                    Convert.ToInt32(dr[6]), 
                                    dr[3].ToString(), 
                                    dr[4].ToString(), 
                                    dr[5].ToString()
                                );
        }

Could someone help me to figure out what I'm doeing wrong? I can't find it. This is the first time I use an DataGridView with comboboxes.


in my experience I found everything seemed to work better if you tied it in through a binding scource, then set the

bindingScource.dataScource.Rows.Add( 
                                    dr[0].ToString(),  
                                    dr[1].ToString(),  
                                    dr[2].ToString(),  
                                    Convert.ToInt32(dr[6]),  
                                    dr[3].ToString(),  
                                    dr[4].ToString(),  
                                    dr[5].ToString() 
                                ); 


Select the correct row? you mean select from the dropdown to see the row inside the datagrid?

 int index = dropdown.SelectedIndex();

 for(int count = 0; count < dgvEenheden.Rows.Count; count ++)
{
   if (dgvEenheden.Rows[count].Cells["<enter col name here>"].Value.ToString().equals(dropdown.Items[index].Text))
{
    dgvEenheden.Rows[count].Selected = true; //to select the Row
    dgvEenheden.Rows[count].Cells[<Cell Number>].Selected = true; //to select the specific Cell

}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号