簡體   English   中英

如何從datagridview單元獲取comboBox值

[英]How to Get comboBox value from a datagridview cell

我想在DataGridView獲取ComboBox單元的值,以將其放入記錄的Update方法中。 ComboBox值與DisplayMember一起顯示,並作為ID存儲在數據庫中。

這是一個ComboBox在我DataGridView與我的形式加載方法ComboBox在指數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();
    }

我從這個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 (“”,“活動”,“NotActive”,“其他”)中的ComboBoxDGV作為ComboBox的值存儲為“ IDx數據庫”。

假設ID是一個整數:

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

我有一個類似的ASP.NET項目,並且使用FindControl查找組合框ID

猜猜您正在使用Form,我會這樣嘗試:

 ComboBox cbo = this.Controls.Find("combotBox1", true).FirstOrDefault() as ComboBox;

Control.ControlCollection.Find方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM