[英]Get ComboBox ValueMember from DataGridview
I'm trying to get the ValueMember of a ComboBox that is in a Datagridview cell from a button click event:我正在尝试从按钮单击事件中获取位于 Datagridview 单元格中的 ComboBox 的 ValueMember:
private void button2_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
OleDbDataAdapter t3DataAdapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM Adok;", conn));
OleDbCommandBuilder t3Command = new OleDbCommandBuilder(t3DataAdapter);
t3DataAdapter.FillSchema(ds, SchemaType.Source, "Adok");
for (int i = 0; i < dataGridView3.Rows.Count; i++)
{
DataRow row4 = ds.Tables["Adok"].NewRow();
row4["HoltID"] = ((ComboBox)dataGridView3["HoltID", i]).ValueMember ;// I would like to get something like this
ds.Tables["Adok"].Rows.Add(row4);
}
}
}
The problem was that I created a ComboBox in CellBeginEdit event, but in the CellEndEdit event I hid it and the value was added to the cell value.问题是我在 CellBeginEdit 事件中创建了一个 ComboBox,但在 CellEndEdit 事件中我隐藏了它,并将值添加到单元格值中。 (So the cell type was a DataGridViewTextBoxCell at the end, not a DataGridViewComboBoxCell) (所以单元格类型最后是 DataGridViewTextBoxCell,而不是 DataGridViewComboBoxCell)
So I used the the Tag property of the cell in CellEndEdit to save also the ID.所以我使用 CellEndEdit 中单元格的 Tag 属性来保存 ID。
Now, everything works.现在,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.