Reputation: 89
I want to get value of a ComboBox
cell in the DataGridView
to put it in a Update method for the record. Values for this ComboBox
are displayed with DisplayMember
and are stored in the database as ID
.
This is a ComboBox
in my DataGridView
in my form load method with the ComboBox
cell at Index 15
.............
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();
}
I am getting values from this DataGridView
to pass it to the Update method like this.....
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);
}
How do I get ValueMember(IDx)
from the DisplayMember
("", "Active", "NotActive", "Other") of the ComboBox
in the DGV
as ComboBox
value is stored as 'IDx
' in the database.
Upvotes: 0
Views: 5194
Reputation: 1718
Assuming that the ID is an 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
If it doesn't work, check with the debugger the types of following items:
this.dgv_Cust.CurrentRow.Cells[15].Value
((DataGridViewComboBoxCell)this.dgv_Cust.CurrentRow.Cells[15]).ValueMember
Upvotes: 1
Reputation: 95
I have similar project for ASP.NET and I use FindControl to find combobox ID
Guess you are using Form, I'd try like that:
ComboBox cbo = this.Controls.Find("combotBox1", true).FirstOrDefault() as ComboBox;
Control.ControlCollection.Find Method
Upvotes: 1