【问题标题】:DataGridViewComboBoxColumn: Get ValueMemberDataGridViewComboBoxColumn:获取 ValueMember
【发布时间】:2015-06-26 18:03:35
【问题描述】:

我有一个带有 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”值

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多