DataGridViewComboBox列名称/值如何?
我认为这就像在 Access 中一样简单。
用户需要将数据表中一列的值设置为 1 或 2。
我想呈现一个组合框,显示“一”、“二”并在幕后设置 1 或 2,就像我在 Access 中多次所做的那样-表格。
另一方面,如果显示表格,则不应显示 1 或 2,而是显示组合框中相应的字符串。
我怎样才能完成这个简单的任务?
I thought this was simple like in Access.
User needs to set the value of one column in a datatable to either 1 or 2.
I wanted to present a combobox showing "ONE", "TWO" and setting 1 or 2 behind the scene, like I did lots of times in Access-Forms.
On the other side, if the table is shown it shall not show 1 or 2 but the corresponding string in the ComboBox.
How can I get this simple task to work??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您指的是 DataGridView,它适用于 Windows 窗体,而 GridView 适用于 ASP.NET,尽管您这样标记了您的问题。
如何将数据绑定到 DataGridViewComboBoxColumn?您需要设置 DisplayMember 和 ValueMember 属性。 DisplayMember 的 MSDN 链接显示了一个示例,但它并不能完全显示您所请求的内容,因为它将两个属性设置为相同的内容。
DisplayMember 将是您希望用户看到的文本,而 ValueMember 将是与其关联的隐藏基础值。
举个例子,假设您的项目中有一个 Choice 类,它代表您的选择,如下所示:
GetChoices() 将返回一个包含您的选择的列表。理想情况下,您应该在服务层中拥有这样的方法,或者如果您愿意的话,您可以在其他地方构建自己的列表(在表单的代码后面)。为了简单起见,我将它们集中在同一类中。
在表单中,您可以将列表绑定到 DataGridViewComboBoxColumn,如下所示:
您现在应该在组合框中看到“One”和“Two”。当您从中获取所选值时,它应该是基础 1 或 2 值。
这就是使用 DisplayMember/ValueMember 背后的想法。这应该会让您继续前进并帮助您调整您正在使用的数据源。
I assume you meant DataGridView, which is for Windows Forms, while the GridView is for ASP.NET although you tagged your question as such.
How are you binding the data to the DataGridViewComboBoxColumn? You'll need to set the DisplayMember and ValueMember properties on the DataGridViewComboBoxColumn while setting its DataSource. The MSDN link to DisplayMember shows an example, but it doesn't quite show what you're requesting since it sets both properties to the same thing.
The DisplayMember would be the text you want the user to see, and the ValueMember would be the hidden underlying value associated with it.
For the sake of an example, let's say you have a Choice class in your project that represents your selections and looks like this:
GetChoices() will return a list containing your choices. Ideally you would have such a method in a service layer, or you could build your own list elsewhere if you wanted to (in your form's code behind). For simplicity I've lumped it all together in the same class.
In your form you would bind the list to the DataGridViewComboBoxColumn as follows:
You should now see "One" and "Two" in the combobox. When you get the selected value from it, it should be the underlying 1 or 2 value.
That's the idea behind using DisplayMember/ValueMember. This should get you going and help you adapt the datasource you were using.
这是当组合框中的值发生变化时从网格中读取值的方式:
来源:这篇文章
This is how you read the value from the grid when the value in the combobox changes:
sources: this post
请注意,在上面给出的示例中,DataGridViewComboBoxColumn cboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[0];不起作用 - 编译器不允许您将 dateGridView 列转换为 DataGridViewComboBoxColumn。我对这个示例很感兴趣,因为它是我见过的少数几个包含本地数据源而不是数据库的示例之一。我的应用程序从文件中读取数据。希望有人看到这一点并更新该示例以在当前版本的 .NET 中工作。
Note that in the example given above, DataGridViewComboBoxColumn cboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[0]; does not work - the compiler won't allow you to cast a dateGridView column to a DataGridViewComboBoxColumn. I'm interested in the example because it's one of the few that I've seen that includes a local data source instead of a database. My application reads data from a file. Hopefully someone sees this and updates the example to work in a current version of .NET.