DataGridViewComboBoxColumn名称/值怎么样?名称、DataGridViewComboBoxColumn

2023-09-03 00:37:26 作者:對妳何止壹句喜歡

我认为这是简单的像在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.

我想present组合框显示一,二,并设置1或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.

在另一侧,如果表示它不应出现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性能,同时设置其数据源。在MSDN链接的DisplayMember显示了一个例子,但它并没有完全展现你的要求是什么,因为它同时设置属性,同样的事情。

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.

为了一个例子,假设你在你的项目中选择类,再presents您的选择,看起来是这样的:

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()将返回包含您的选择列表。理想情况下,你会有这样一种方法,在服务层,或者你可以建立自己的列表在其他地方,如果你想(在表单的code后面)。为简单起见,我集中它一起在同一个班级。

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.