DataGridViewComboBoxColumn的名称/值怎么样? [英] DataGridViewComboBoxColumn name/value how?

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

问题描述



用户需要将datatable中的一列的值设置为1或2。



我想提出一个显示ONE,TWO的组合框,并在场景后设置1或2,就像我在Access-Forms中做了很多次。



另一方面,如果表格显示,它不会显示1或2,但ComboBox中的相应字符串。



如何我可以让这个简单的任务工作吗?

解决方案

我认为你的意思是DataGridView,这是Windows窗体,而GridView对于ASP.NET,虽然你标记了你的问题。



如何将数据绑定到DataGridViewComboBoxColumn?您需要设置 DisplayMember 和ValueMember属性在DataGridViewComboBoxColumn设置其DataSource时。与DisplayMember的MSDN链接显示了一个示例,但它并不能显示您要求的内容,因为它将两个属性都设置为相同的东西。



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



为了一个例子,让我们说你有一个选择您的项目中的类代表您的选择,如下所示:

  public class Choice 
{
public string Name {get;私人集合}
public int Value {get;私人集合
public Choice(string name,int value)
{
Name = name;
Value = value;
}

私人静态只读列表<选择> possibleChoices =新列表<选择>
{
{new Choice(One,1)},
{new Choice(Two,2)}
};

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

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



在表单中,您将按照以下方式将列表绑定到DataGridViewComboBoxColumn:

  //引用组合框列
DataGridViewComboBoxColumn cboBoxColumn =(DataGridViewComboBoxColumn)dataGridView1.Columns [0];
cboBoxColumn.DataSource = Choice.GetChoices();
cboBoxColumn.DisplayMember =Name; // Choice类中的Name属性
cboBoxColumn.ValueMember =Value; //同上的Value属性

您现在应该在组合框中看到One和Two 。当您从中获取所选值时,它应该是基本的1或2值。



这是使用DisplayMember / ValueMember的想法。这应该让你去帮助你适应你使用的数据源。


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

解决方案

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:

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() 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:

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

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.

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

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