Reputation: 159
I am developing a Windows Form application in C#. I have a DataGridView which displays data from a table. I populate the grid using a Data table and I have a Combobox Column added to the DataGridView . I need to preset the Combox value based on the value of another column of the grid. However inside the DataBindingComplete event of the DataGridView where I want to set the Idex of the Combobox I cant see the newly added combobox column. Following code would better explain the thing.
Code to load the DataGridView.
DataTable family = new DataTable();
family.Columns.Add("tin", typeof(string));
family.Columns.Add("ftin", typeof(string));
family.Columns.Add("fullname", typeof(string));
family.Columns.Add("age", typeof(string));
family.Columns.Add("gender", typeof(string));
family.Columns.Add("fathername", typeof(string));
family.Columns.Add("mothername", typeof(string));
family.Columns.Add("relationshipid", typeof(string));
family.Columns.Add("address", typeof(string));
family.Columns.Add("cat", typeof(string));
//read data
cmd.CommandText = "SELECT fullname,fathername,mothername,relationshipid,tin,ftin,address,age,gender FROM nprmembers where ftin='" + s.Trim() + "' Order By tin ";
var reader = cmd.ExecuteReader();
//
while (reader.Read())
{
string gndr = GetGender(reader.GetString(8).Trim());
family.Rows.Add(reader.GetString(4).TrimStart(),
reader.GetString(5).TrimStart(), reader.GetString(0).TrimStart(),
reader.GetString(7).TrimStart(), gndr, reader.GetString(1).TrimStart(),
reader.GetString(2).TrimStart(), reader.GetString(3).TrimStart(),
reader.GetString(6).TrimStart(),"2");
}
reader.Close();
reader.Dispose();
//Set Data Source
dtGrdViewFamily.DataSource = family;
dtGrdViewFamily.AutoGenerateColumns = false;
dtGrdViewFamily.Columns[0].HeaderText = "TIN";
dtGrdViewFamily.Columns[0].Width = 180;
dtGrdViewFamily.Columns[1].Visible = false;
dtGrdViewFamily.Columns[2].HeaderText = "Name";
dtGrdViewFamily.Columns[2].Width = 180;
dtGrdViewFamily.Columns[3].HeaderText = "DOB";
dtGrdViewFamily.Columns[3].Width = 70;
dtGrdViewFamily.Columns[4].HeaderText = "Gender";
dtGrdViewFamily.Columns[4].Width = 60;
dtGrdViewFamily.Columns[5].HeaderText = "Father";
dtGrdViewFamily.Columns[6].HeaderText = "Mother";
dtGrdViewFamily.Columns[7].HeaderText = "Relation";
dtGrdViewFamily.Columns[8].HeaderText = "Address";
dtGrdViewFamily.Columns[9].HeaderText = "cat";
dtGrdViewFamily.Columns[9].Width = 2;
dtGrdViewFamily.Columns[9].Visible = false;
Here I add new Column
//I have added this to stop repeat of this column every time a new set of data is loaded to the grid
if (dtGrdViewFamily.ColumnCount == 10)
{
//Category
DataGridViewComboBoxColumn rcCategories = new DataGridViewComboBoxColumn();
rcCategories.HeaderText = "Category";
rcCategories.DataSource = GetRCCategories();
rcCategories.DisplayMember = "Values";
rcCategories.ValueMember = "Keys";
dtGrdViewFamily.Columns.Add(rcCategories);
}
At this point the grid is visible with a Combobox with a set of values. Now I want to set Combobox valuees of each rows of the grid with the content of another column of the grid. So I call dtGrdViewFamily_DataBindingComplete event. The code is
private void dtGrdViewFamily_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach(DataGridViewRow row in dtGrdViewFamily.Rows)
{
if(!row.IsNewRow)
{
row.Cells["Category"].Value = row.Cells["cat"].Value;
//AT THIS POINT it FAILS TO IDENTIFY THE CATEGORY FIELD
}
}
}
The debugger shows only 10 fields (0-9) of the dtGrdViewFamily. The Category combobox field is missing. Why so? and how I achieve the objective of preselecting value of the combobox. Thanks
Upvotes: 0
Views: 1271
Reputation: 159
Got a solution. The problem seems, DataBindingComplete is called before Combox of the grid are loaded. So I shifted the code for adding combox to the grid to DataBindingComplete event an used a DataGridViewComboBoxCell instead of DataGridViewComboBoxColumn. The changed code for DatabindingComplete is -
private void dtGrdViewFamily_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//if(!(dtGrdViewFamily.Columns.Contains("Category"))
// return;
foreach(DataGridViewRow row in dtGrdViewFamily.Rows)
{
if (!row.IsNewRow)
{
DataGridViewComboBoxCell rcCategories = new DataGridViewComboBoxCell();
rcCategories.DataSource = GetRCCategories();
rcCategories.DisplayMember = "Values";
rcCategories.ValueMember = "Keys";
row.Cells[9] = rcCategories;
}
}
}
It works fine now.
Upvotes: 0