单击组合框时如何在gridview中插入值

时间:2014-01-09 05:13:24

标签: c# winforms datagridview

private void cmbInNa_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        DataTable dt1 = pbl.getinnm(this.cmbInNa.Text.ToString());
        if (dt1 != null) //&& dt.Rows.Count > 0)
        {
            dataGridView1.DataSource = dt1;

            DataGridViewComboBoxColumn cmbkw = dataGridView1.Columns["cmbkw"] as DataGridViewComboBoxColumn;
            cmbkw.DataSource = dt1;
            cmbkw.DisplayMember = "KeyWord";

            DataGridViewComboBoxColumn cmbRetu = dataGridView1.Columns["cmbRetu"] as DataGridViewComboBoxColumn;

            cmbRetu.DataSource = dt1;
            cmbRetu.DisplayMember = "col_Return";
        } 
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    } 
}

当我单击gridview时,如何在combobox中插入值,现在我可以使用上面的代码从数据库中检索数据,就像我想要插入的那样,当用户选择{时更新值{1}}。

2 个答案:

答案 0 :(得分:0)

像这样添加行

        DataGridView dg = new DataGridView();
        //dg is your Datagridview
        DataGridViewRow dgr = new DataGridViewRow();
        dg.Rows.Add(dgr);

答案 1 :(得分:0)

这样做:

(dataGridView1.DataSource as DataTable).Rows.Add((dataGridView1.DataSource as DataTable).NewRow());