DataGridViewComboBoxColumn 名称/值如何? [英] DataGridViewComboBoxColumn name/value how?

查看:17
本文介绍了DataGridViewComboBoxColumn 名称/值如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这就像在 Access 中一样简单.

I thought this was simple like in Access.

用户需要将数据表中某一列的值设置为 1 或 2.

User needs to set the value of one column in a datatable to either 1 or 2.

我想展示一个组合框,显示一个"、两个"并在幕后设置 1 或 2,就像我在 Access-Forms 中做过很多次一样.

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.

另一方面,如果显示表格,则不应显示 1 或 2,而是显示 ComboBox 中的相应字符串.

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??

推荐答案

我假设您的意思是 DataGridView,它用于 Windows 窗体,而 GridView 用于 ASP.NET,尽管您将问题标记为这样.

I assume you meant DataGridView, which is for Windows Forms, while the GridView is for ASP.NET although you tagged your question as such.

如何将数据绑定到 DataGridViewComboBoxColumn?您需要设置 DisplayMember 和 DataGridViewComboBoxColumn 上的 ValueMember 属性,同时设置其数据源.DisplayMember 的 MSDN 链接显示了一个示例,但它并没有完全显示您的请求,因为它将两个属性设置为相同的内容.

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.

DisplayMember 将是您希望用户看到的文本,ValueMember 将是与其关联的隐藏底层值.

The DisplayMember would be the text you want the user to see, and the ValueMember would be the hidden underlying value associated with it.

举个例子,假设您的项目中有一个 Choice 类,它代表您的选择,如下所示:

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:

public class Choice
{
    public string Name { get; private set; }
    public int Value { get; private set; }
    public Choice(string name, int value)
    {
        Name = name;
        Value = value;
    }

    private static readonly List<Choice> possibleChoices = new List<Choice>
    {
        { new Choice("One", 1) },
        { new Choice("Two", 2) }
    };

    public static List<Choice> GetChoices()
    {
        return possibleChoices;
    }
}

GetChoices() 将返回一个包含您的选择的列表.理想情况下,您可以在服务层中使用这样的方法,或者您可以根据需要在其他地方构建自己的列表(在表单的代码后面).为简单起见,我将它们放在同一个类中.

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.

在您的表单中,您可以将列表绑定到 DataGridViewComboBoxColumn,如下所示:

In your form you would bind the list to the DataGridViewComboBoxColumn as follows:

// reference the combobox column
DataGridViewComboBoxColumn cboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[0];
cboBoxColumn.DataSource = Choice.GetChoices();
cboBoxColumn.DisplayMember = "Name";  // the Name property in Choice class
cboBoxColumn.ValueMember = "Value";  // ditto for the Value property

您现在应该在组合框中看到一"和二".当你从中得到选定的值时,它应该是底层的 1 或 2 值.

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.

这就是使用 DisplayMember/ValueMember 背后的想法.这应该可以帮助您适应正在使用的数据源.

That's the idea behind using DisplayMember/ValueMember. This should get you going and help you adapt the datasource you were using.

这篇关于DataGridViewComboBoxColumn 名称/值如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆