如何更改动态添加的 DataGridViewComboBoxColumn 的组合框单元格的文本?
我对 C# 还是有点陌生,但我正在使用 Winforms,并且我有一个连接到数据源并且正在正确填充的 DataGridView。
我还在运行时添加了一个 ComboBoxColumn。此 ComboBoxColumn 连接到数据源,设置显示成员和值成员,设置标题文本,然后将该列添加到数据网格。连接工作正常,当用户单击组合框时,会按预期填充。
当用户在网格中完成新行时,他显然会选择组合框中的项目,然后完成后整行都会更新到我的数据库中。
我的问题是:当再次打开或重新绘制此数据网格时,由于数据源属性,它会被填充,但是如何让组合框单元格显示他最初选择的值而不是只是一个空白框。我知道从数据库获取值并输入值的代码。理想的情况是我可以将该变量放在组合框的显示中。请记住,组合框仍然是数据绑定的,以便用户可以根据需要编辑值?
我知道在普通的组合框控件中我应该只设置 .Text 属性。但 DataGridViewComboBox 没有相同的属性。
下面是实际数据连接和添加comboBoxColumn 的代码:
public void AddComboBoxColumn(DataGridView datagridName, DataTable table, string headerText, int columnIndex)
{
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
GetDisplayAndValueMembers(table, headerText); //Calls method that gets the datasource, displaymember, valuemember depending on column
column.DataSource = tableRef; //sets datasource to table referenced by column
column.DisplayMember = displayMember; //sets displaymember
column.ValueMember = valueMember; //sets valuemember
column.HeaderText = headerText; //changes headertext to displayed text
if (newColumnIndex == 0)
datagridName.Columns.Add(column); //added to end of datagrid
else
{
datagridName.Columns.RemoveAt(columnIndex);
datagridName.Columns.Insert(newColumnIndex, column); //added in specific index if needed
}
}
这仅显示了下拉列表的数据连接。显然,有很多方法和大量代码一起使用。但这不是问题,因为它工作得很好。我不知道如何解决实际将先前选择的项目显示为组合框文本的问题?
I am still a little new to C# but I am using Winforms and I have a DataGridView which is connected to a datasource and is being populated correctly.
I have also added a ComboBoxColumn at run time. This ComboBoxColumn is being connected to a datasource, the displaymember and valuemember are set, the headertext is set and then the column is added to the datagrid. The connection works just fine and when the user clicks the comboBox is populated as expected.
When a user completes a new row in the grid he will obviously select the item in the comboBox and then the whole row is updated to my database when done.
My question is: When this datagrid is opened again or redrawn it is populated due to the datasource property, but how do I get the comboBox cell to display the VALUE he selected originally instead of just a blank box. I know the code of getting the value from the DB and put in a value. The ideal would be if I could put that variable in the display of the comboBox. Keep in mind that the comboBox is still data bound so that the user could edit the value if he so wishes?
I know that in a normal comboBox control I should just set the .Text property. But the DataGridViewComboBox does not have the same property.
Here is the code for the actual data connection and adding of the comboBoxColumn:
public void AddComboBoxColumn(DataGridView datagridName, DataTable table, string headerText, int columnIndex)
{
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
GetDisplayAndValueMembers(table, headerText); //Calls method that gets the datasource, displaymember, valuemember depending on column
column.DataSource = tableRef; //sets datasource to table referenced by column
column.DisplayMember = displayMember; //sets displaymember
column.ValueMember = valueMember; //sets valuemember
column.HeaderText = headerText; //changes headertext to displayed text
if (newColumnIndex == 0)
datagridName.Columns.Add(column); //added to end of datagrid
else
{
datagridName.Columns.RemoveAt(columnIndex);
datagridName.Columns.Insert(newColumnIndex, column); //added in specific index if needed
}
}
This just shows the dataconnection of the drop down. Obviously there are many methods being used with a large amount of code. But this is not the problem as this works fine. I don't know how to go about the issue of actually showing a previously selected item as the text of the comboBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您正在寻找的是
ComboBoxColumn
的DataPropertyName
属性。此属性创建DataGridView
的DataSource
与ComboBoxColumn
的选定值之间的绑定。例如,假设您有一个显示在组合框中的产品列表。然后,您的
DataGridView
DataSource
中也会有一个 ProductId。类似这样:设置了
DataPropertyName
后,您现在将在ComboBoxColumn
和DataGridView
之间进行数据绑定。如果您的基础数据源绝对不能包含组合框属性的值,那么您将需要在自定义代码中处理
ComboBoxColumn
的所有保存和值设置。要设置
DataGridViewComboBoxCell
的选定值,您只需选择该单元格,然后设置其Value
属性即可。It sounds like what you are looking for is the
DataPropertyName
property of theComboBoxColumn
. This property creates the binding between theDataSource
of theDataGridView
and the selected value of theComboBoxColumn
.For example, say you have a list of Products displayed in the combo boxes. You would then also have a ProductId in your
DataGridView
DataSource
. Something like this:With the
DataPropertyName
set you now will have databinding between theComboBoxColumn
and theDataGridView
.If you underlying data source absolutely cannot have the value of the combo box property in it then you will need to handle all of this saving and value setting of the
ComboBoxColumn
within custom code.To set the selected value of a
DataGridViewComboBoxCell
all you do is select the cell and then set itsValue
property.