组合框是不是在C#中的DataGridView显示的默认值

组合框是不是在C#中的DataGridView显示的默认值

本文介绍了组合框是不是在C#中的DataGridView显示的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户从DataGridViewComboBoxColumn每个新行选择值。我势必组合框在GridView的数据库,但每当我进入新行我看DataGridViewComboBoxColumn没有初始值。我必须先设置的值。



我怎样才能让缺省值出现在DataGridViewComboBoxColumn每当我在DataGridView中输入新行?



这是怎么我结合DataGridView的组合框数据库:

  bindingSource1.DataSource = itemBAL.GetTable(); 
dataGridView1.DataSource = bindingSource1;
ItemName.DataSource = bindingSource1; //其中ITEMNAME是一个实例ofDataGridViewComboBoxColumn
ItemName.DisplayMember =姓名;
ItemName.ValueMember =ITEMID;


解决方案

您可以使用 DefaultCellStyle .NullValue DefaultCellStyle.DataSourceNullValue 组合框列的属性。



有在这个 rel=\"nofollow\">一个比较全面的MSDN文章。



我也给下面的代码示例:

  //比方说,我有这汽车>汽车我要添加到我的datagridview 
名单,LT名单;汽车=新的List<汽车>();
cars.Add(新车(){指数= 0,使=福特});
cars.Add(新车(){指数= 1,使=Chevvy});
cars.Add(新车(){指数= 2,使=丰田});

//创建列,设置所有的各种属性
DataGridViewComboBoxColumn列=新DataGridViewComboBoxColumn();
col.DataSource =车;
col.Name =汽车总动员;
col.DisplayMember =制作;
col.HeaderText =汽车;
col.ValueMember =指数;
col.DataPropertyName =汽车;

//除了为我设置这两个组合框列标准属性:
col.DefaultCellStyle.NullValue =汽车[0]。使;
col.DefaultCellStyle.DataSourceNullValue =汽车[0]的.index;

dataGridView1.Columns.Add(COL);

dataGridView1.DataSource = DT;

有一件事情与上面的代码需要注意的是,我设置了 DataPropertyName ,以便与在DataGridView的DataSource属性绑定。



如果我不这样做,那么我会在访问时需要添加一些额外的逻辑当用户没有选择,因为属性的值将是空的组合框列(NullValue属性不设置一个值,实际的细胞,只显示了一个默认值)。


I'm trying to allow user to select value from DataGridViewComboBoxColumn for each new row. I've bound Combo Box 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显示的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 10:12