C# DataGridViewComboBoxColumn 名称/值如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1390462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridViewComboBoxColumn name/value how?
提问by
I thought this was simple like in Access.
我认为这很简单,就像在 Access 中一样。
User needs to set the value of one column in a datatable to either 1 or 2.
用户需要将数据表中一列的值设置为 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,就像我在 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.
另一方面,如果显示表格,则不应显示 1 或 2,而是显示 ComboBox 中的相应字符串。
How can I get this simple task to work??
我怎样才能让这个简单的任务工作?
采纳答案by Ahmad Mageed
I assume you meant DataGridView, which is for Windows Forms, while the GridView is for ASP.NET although you tagged your question as such.
我假设您的意思是 DataGridView,它用于 Windows 窗体,而 GridView 用于 ASP.NET,尽管您将问题标记为这样。
How are you binding the data to the DataGridViewComboBoxColumn? You'll need to set the DisplayMemberand 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.
您如何将数据绑定到 DataGridViewComboBoxColumn?在设置 DataSource 时,您需要在 DataGridViewComboBoxColumn 上设置DisplayMember和 ValueMember 属性。DisplayMember 的 MSDN 链接显示了一个示例,但它并没有完全显示您的请求,因为它将两个属性设置为相同的内容。
The DisplayMember would be the text you want the user to see, and the ValueMember would be the hidden underlying value associated with it.
DisplayMember 将是您希望用户看到的文本,而 ValueMember 将是与其关联的隐藏底层值。
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:
举个例子,假设您的项目中有一个 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() 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.
GetChoices() 将返回一个包含您的选择的列表。理想情况下,您将在服务层中使用这样的方法,或者您可以根据需要在其他地方构建自己的列表(在表单的代码后面)。为简单起见,我将它们放在同一个类中。
In your form you would bind the list to the DataGridViewComboBoxColumn as follows:
在您的表单中,您可以将列表绑定到 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
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.
您现在应该在组合框中看到“一”和“二”。当您从中获得选定的值时,它应该是底层的 1 或 2 值。
That's the idea behind using DisplayMember/ValueMember. This should get you going and help you adapt the datasource you were using.
这就是使用 DisplayMember/ValueMember 背后的想法。这应该可以帮助您适应正在使用的数据源。
回答by imjoris
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
来源:这篇文章