我正在使用C#开发Windows窗体应用程序。我有一个显示表中数据的DataGridView。我使用Data表填充网格,并在DataGridView中添加了Combobox列。我需要根据网格的另一列的值预设Combox值。但是在DataGridView的DataBindingComplete事件中,我想要设置Combobox的Idex,我看不到新添加的组合框列。以下代码可以更好地解释这个问题。
加载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));
//读取数据
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();
//设置数据源
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;
这里我添加新列
//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);
}
此时,使用带有一组值的Combobox可以看到网格。 现在我想将网格的每一行的Combobox值设置为网格的另一列的内容。所以我调用dtGrdViewFamily_DataBindingComplete事件。代码是
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
}
}
}
调试器仅显示dtGrdViewFamily的10个字段(0-9)。缺少类别组合框字段。 为什么这样?以及我如何实现预选组合框的价值的目标。感谢
答案 0 :(得分:0)
有一个解决方案。 问题似乎是,在加载网格的Combox之前调用DataBindingComplete。 所以我将用于添加combox的代码移动到网格到DataBindingComplete事件,并使用了DataGridViewComboBoxCell而不是DataGridViewComboBoxColumn。 DatabindingComplete的更改代码是 -
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;
}
}
}
现在工作正常。