我希望获得ComboBox
中DataGridView
单元格的值,以将其放入记录的Update方法中。此ComboBox
的值随DisplayMember
一起显示,并以ID
的形式存储在数据库中。
我的表单加载方法ComboBox
中的DataGridView
,索引为15的ComboBox
单元格
.............
dgv_Cust.Columns[14].Name="Web";
dgv_Cust.Columns[14].DataPropertyName = "Web";
DataGridViewComboBoxColumn cbcol = new DataGridViewComboBoxColumn();
cbcol.Name = "AccStatus";
cbcol.DataPropertyName = "cbcol"; // DataTable column name
cbcol.Items.Add(new ComboRecord(0, ""));
cbcol.Items.Add(new ComboRecord(1, "Active"));
cbcol.Items.Add(new ComboRecord(2, "NotActive"));
cbcol.Items.Add(new ComboRecord(3, "Other"));
cbcol.DisplayMember = "Status";
cbcol.ValueMember = "IDx";
cbcol.DisplayIndex = 15;
dgv_Cust.Columns.Add(cbcol);
dgv_Cust.Columns[15].Name = "AccStatus";
dgv_Cust.Columns[15].DataPropertyName = "AccStatus";
dgv_Cust.ColumnCount = 17;
dgv_Cust.Columns[16].Name="Location";
dgv_Cust.Columns[16].DataPropertyName = "Location";
dgv_Cust.DataSource = DBConn.getCustInfo1();
}
我从这个DataGridView
获取值,将其传递给Update方法,就像这样......
private void button2_Click(object sender, EventArgs e) //UpdateCustomer
{
//Here I want to get valueMember(IDx) for cell[15], from the comboBox
string PostalCode = this.dgv_Cust.CurrentRow.Cells[2].Value.ToString();
string Name = this.dgv_Cust.CurrentRow.Cells[3].Value.ToString();
string BusinessName = this.dgv_Cust.CurrentRow.Cells[4].Value.ToString();
string AltName = this.dgv_Cust.CurrentRow.Cells[5].Value.ToString();
string Adrs1 = this.dgv_Cust.CurrentRow.Cells[6].Value.ToString();
string Adrs2 = this.dgv_Cust.CurrentRow.Cells[7].Value.ToString();
string City = this.dgv_Cust.CurrentRow.Cells[8].Value.ToString();
string Province = this.dgv_Cust.CurrentRow.Cells[9].Value.ToString();
string Phone1 = this.dgv_Cust.CurrentRow.Cells[10].Value.ToString();
string Phone2 = this.dgv_Cust.CurrentRow.Cells[11].Value.ToString();
string Email1 = this.dgv_Cust.CurrentRow.Cells[12].Value.ToString();
string Email2 = this.dgv_Cust.CurrentRow.Cells[13].Value.ToString();
string Web = this.dgv_Cust.CurrentRow.Cells[14].Value.ToString();
string Location = this.dgv_Cust.CurrentRow.Cells[16].Value.ToString();
try
{
DBConn.updateCustInfo1(AccStatus, PostalCode, Name, BusinessName, AltName, Adrs1, Adrs2, City, Province, Phone1, Phone2, Email1, Email2, Web, Location);
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
如何从ValueMember(IDx)
中DisplayMember
的{{1}}(“”,“有效”,“NotActive”,“其他”)获取ComboBox
作为DGV
ComboBox
值在数据库中存储为“IDx
”。
答案 0 :(得分:1)
假设ID是int:
ComboRecord cr =((DataGridViewComboBoxCell)this.dgv_Cust.CurrentRow.Cells[15]).ValueMember;
int id = cr.xxx ; // replace xxx by the Id property/variable of the ComboRecord
如果它不起作用,请使用调试器检查以下项目的类型:
this.dgv_Cust.CurrentRow.Cells[15].Value
((DataGridViewComboBoxCell)this.dgv_Cust.CurrentRow.Cells[15]).ValueMember
答案 1 :(得分:1)
我有类似的ASP.NET项目,我使用FindControl查找组合框ID
猜猜你在使用Form,我会这样尝试:
ComboBox cbo = this.Controls.Find("combotBox1", true).FirstOrDefault() as ComboBox;