DataGridViewComboBox列名称/值如何?

发布于 2024-08-04 06:37:27 字数 197 浏览 8 评论 0原文

我认为这就像在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生殊途 2024-08-11 06:37:27

我假设您指的是 DataGridView,它适用于 Windows 窗体,而 GridView 适用于 ASP.NET,尽管您这样标记了您的问题。

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

DisplayMember 将是您希望用户看到的文本,而 ValueMember 将是与其关联的隐藏基础值。

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

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() 将返回一个包含您的选择的列表。理想情况下,您应该在服务层中拥有这样的方法,或者如果您愿意的话,您可以在其他地方构建自己的列表(在表单的代码后面)。为了简单起见,我将它们集中在同一类中。

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

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

您现在应该在组合框中看到“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:

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.

梦途 2024-08-11 06:37:27

这是当组合框中的值发生变化时从网格中读取值的方式:

dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
    {
        ComboBox comboBox = e.Control as ComboBox;
        comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
    }
}

private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
    var sendingCB = sender as DataGridViewComboBoxEditingControl;
    object value = sendingCB.SelectedValue;
    if (value != null)
    {
        int intValue = (int)sendingCB.SelectedValue;
        //do something with value
    }
}

来源:这篇文章

This is how you read the value from the grid when the value in the combobox changes:

dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
    {
        ComboBox comboBox = e.Control as ComboBox;
        comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
    }
}

private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
    var sendingCB = sender as DataGridViewComboBoxEditingControl;
    object value = sendingCB.SelectedValue;
    if (value != null)
    {
        int intValue = (int)sendingCB.SelectedValue;
        //do something with value
    }
}

sources: this post

雪花飘飘的天空 2024-08-11 06:37:27

请注意,在上面给出的示例中,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文