组合框在C#中的DataGridView中未显示默认值 [英] Combo box is not displaying default value in DataGridView in C#
问题描述
我试图允许用户从每个新行的 DataGridViewComboBoxColumn
中选择值。我已经在 GridView
中将 ComboBox
绑定到数据库,但每当我输入新行,我看到没有初始值的DataGridViewComboBoxColumn
我必须首先设置值。
当我在 DataGridView中输入新行时,如何让默认值出现在
? DataGridViewComboBoxColumn
中
这是我如何绑定 DataGridView
ComboBox
到数据库:
bindingSource1.DataSource = itemBAL.GetTable();
dataGridView1.DataSource = bindingSource1;
ItemName.DataSource = bindingSource1; //其中ItemName是DataGridViewComboBoxColumn的一个实例
ItemName.DisplayMember =Name;
ItemName.ValueMember =ItemId;
您可以使用 DefaultCellStyle .NullValue
和 DefaultCellStyle.DataSourceNullValue
组合框列的属性。
关于此此处的相当全面的MSDN文章。
我也提供了一个代码示例:
//假设我有这个我想要的汽车列表添加到我的datagridview
列表< Car> cars = new List< Car>();
cars.Add(new Car(){Index = 0,Make =Ford});
cars.Add(new Car(){Index = 1,Make =Chevvy});
cars.Add(new Car(){Index = 2,Make =Toyota});
//我创建列,设置所有各种属性
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = cars;
col.Name =汽车;
col.DisplayMember =Make;
col.HeaderText =Car;
col.ValueMember =Index;
col.DataPropertyName =Car;
//除了组合框列的标准属性外,我设置了这两个:
col.DefaultCellStyle.NullValue = cars [0]。
col.DefaultCellStyle.DataSourceNullValue = cars [0] .Index;
dataGridView1.Columns.Add(col);
dataGridView1.DataSource = dt;
上面的代码需要注意的一点是,我正在设置 DataPropertyName
允许与datagridview的数据源上的属性绑定。
如果我没有这样做,我需要在访问时添加一些额外的逻辑当用户没有选择任何内容时,组合框列,因为该属性的值将为null(NullValue不将值设置为实际单元格,仅显示默认值)。
I'm trying to allow user to select value from DataGridViewComboBoxColumn
for each new row. I've bound ComboBox
in GridView
to database, but whenever I enter new row I see DataGridViewComboBoxColumn
with no initial value. I have to set the value first.
How can I let default value appear in DataGridViewComboBoxColumn
whenever I enter new row in DataGridView
?
This is how I'm binding DataGridView
ComboBox
to database :
bindingSource1.DataSource = itemBAL.GetTable();
dataGridView1.DataSource = bindingSource1;
ItemName.DataSource = bindingSource1; //Where ItemName is an instance ofDataGridViewComboBoxColumn
ItemName.DisplayMember = "Name";
ItemName.ValueMember = "ItemId";
You can use the DefaultCellStyle.NullValue
and DefaultCellStyle.DataSourceNullValue
properties of the combo box column.
There is a quite comprehensive MSDN article on this here.
I've also given a code example below:
// Let's say I have this list of cars I want to add to my datagridview
List<Car> cars = new List<Car>();
cars.Add(new Car() { Index = 0, Make = "Ford" });
cars.Add(new Car() { Index = 1, Make = "Chevvy" });
cars.Add(new Car() { Index = 2, Make = "Toyota" });
// I create the column, setting all the various properties
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = cars;
col.Name = "Cars";
col.DisplayMember = "Make";
col.HeaderText = "Car";
col.ValueMember = "Index";
col.DataPropertyName = "Car";
// As well as standard properties for a combobox column I set these two:
col.DefaultCellStyle.NullValue = cars[0].Make;
col.DefaultCellStyle.DataSourceNullValue = cars[0].Index;
dataGridView1.Columns.Add(col);
dataGridView1.DataSource = dt;
One thing to note with the code above is that I am setting the DataPropertyName
to allow binding with a property on the datagridview's datasource.
If I was not doing that then I would need to add some additional logic when accessing the combo box column when users have selected nothing, since the Value of the property would be null (the NullValue does not set a value to the actual cell, only shows a default value).
这篇关于组合框在C#中的DataGridView中未显示默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!