DataGridViewComboBoxColumn:获取ValueMember

时间:2015-06-26 18:03:35

标签: c# winforms datagridview

我有一个带有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,或“描述”我需要相应的“NUM”值

1 个答案:

答案 0 :(得分:0)

啊。经过更多实验后很容易:

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