DataGridViewComboBoxColumn: Get ValueMember

本文关键字:ValueMember Get DataGridViewComboBoxColumn | 更新日期: 2023-09-27 18:00:02

我有一个带有DataGridViewComboBoxColumn的DataGrid,它有一个数据源。我需要从所选项目中获取ValueMember。以下是绑定它的代码:

        private void DgvWorkBars_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
        string connectionString = Settings.Default.ProdConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connection;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT DESCRIPTION,NUM FROM dbo.DIMENSIONS WHERE DIMENSIONCODE = 1";
        SqlDataAdapter sqlDa = new SqlDataAdapter();
        sqlDa.SelectCommand = cmd;
        DataSet ds = new DataSet();
            sqlDa.Fill(ds, "DIMENSIONS");
            DataRow nRow = ds.Tables["DIMENSIONS"].NewRow();
            ds.Tables["DIMENSIONS"].Rows.InsertAt(nRow, 0);
            //Binding the data to the combobox.
            ClnDivision.DataSource = ds.Tables["DIMENSIONS"].DefaultView;
            connection.Close();
            ClnDivision.DisplayMember =
                ds.Tables["DIMENSIONS"].Columns["DESCRIPTION"].ToString();
            ClnDivision.ValueMember =
                ds.Tables["DIMENSIONS"].Columns["NUM"].ToString();
   }

我试着用

string division = ((DataGridView)sender)[0,e.RowIndex].EditedFormattedValue.ToString();

这给了我DisplayMember或"Description",我需要相应的"NUM"值

DataGridViewComboBoxColumn: Get ValueMember

ugh。经过更多实验后变得容易:

((DataGridView)sender)[0,e.RowIndex].Value.ToString();