我有一个dataGridView,其列类型之一是ComboBox。该comboBox与dataSource绑定。问题是,当我使用以下行为此comboBox设置值时:
for (int i = 0; i < lstData.Count; i++)
{
grd["ColumnName",i].Value = lstData[i].Data;
}
或使用以下行:
grd.Rows.Add(lstData[i].Data)
它应该显示Short_Description(0 - Reg)而不是Report_Code(0)。但它将值设置为Text to dropdown而不是设置Value。我错过了什么? 附上屏幕截图以供参考。
答案 0 :(得分:0)
public partial class GridManualBind : Form
{
//Binding Source for datagridView
BindingSource datasource = new BindingSource();
//Binding Source for datagridView Combobox Column
BindingSource reportLookupBinding = new BindingSource();
List<ManualData> list = new List<ManualData>();
public GridManualBind()
{
InitializeComponent();
dataGridView1.AutoGenerateColumns = false;
datasource.DataSource = typeof(ManualData);
reportLookupBinding.DataSource = typeof(ManualDataReport);
dataGridView1.DataSource = datasource;
// column
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn
{
HeaderText = "Price Type",
DataPropertyName = "PriceType"
});
// As you see in the picture its configured for combo box column
dataGridView1.Columns.Add(new DataGridViewComboBoxColumn
{
HeaderText = "Report Code Lookup",
DataPropertyName = "ReportCodeLookup",
DataSource = reportLookupBinding,
DisplayMember = "Short_Description",
ValueMember = "Report_Code"
});
//Adding Data for Combo Box data Source
reportLookupBinding.Add(new ManualDataReport { Report_Code = 0, Short_Description = "0-Reg" });
reportLookupBinding.Add(new ManualDataReport { Report_Code = 1, Short_Description = "1-Post" });
list.AddRange(new ManualData[]
{
new ManualData { PriceType="Reg", ReportCodeLookup=0 },
new ManualData { PriceType="Post", ReportCodeLookup=1 }
});
//Adding Data for grid View data Source
datasource.Add(list[0]);
datasource.Add(list[1]);
}
private void button1_Click(object sender, EventArgs e)
{
// you can Set the combo box value using this;
list[0].ReportCodeLookup = 1;
dataGridView1.Invalidate();
}
private class ManualData
{
public string PriceType { get; set; }
public int ReportCodeLookup { get; set; }
}
private class ManualDataReport
{
public int Report_Code { get; set; }
public string Short_Description { get; set; }
}
}
设置组合框单元格值的值
private void button1_Click(object sender, EventArgs e)
{
// you can Set the combo box value using this;
list[0].ReportCodeLookup = 1; // depend on combo box cell value
dataGridView1.Invalidate();
}