Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datagridView中的下拉列表_C#_Visual Studio 2013_Datagridview_Combobox_Ssms - Fatal编程技术网

C# datagridView中的下拉列表

C# datagridView中的下拉列表,c#,visual-studio-2013,datagridview,combobox,ssms,C#,Visual Studio 2013,Datagridview,Combobox,Ssms,您好,我在windows窗体中有2个datagridview,在1个datagridview中有一个组合框,如下所示 当我更改组合框的值时,datagridview中的表应该更新。例如,如果我选择第二个组合框作为outputmetadatafield1,则该行的列应该用输出元数据表中第一行的列更新。到目前为止,我无法向组合框添加一个侦听器作为下拉列表。\u selectedindexchanged.Can有人告诉m,e怎么做吗 public partial class Form1 : Form

您好,我在windows窗体中有2个datagridview,在1个datagridview中有一个组合框,如下所示

当我更改组合框的值时,datagridview中的表应该更新。例如,如果我选择第二个组合框作为outputmetadatafield1,则该行的列应该用输出元数据表中第一行的列更新。到目前为止,我无法向组合框添加一个侦听器作为下拉列表。\u selectedindexchanged.Can有人告诉m,e怎么做吗

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        load_input_table();
        load_output_table();
    }
    static String conn = @"Data Source=SUMEET-PC\MSSQLSERVER1;Initial Catalog=EMIDS;Integrated Security=True";
    SqlConnection connection = new SqlConnection(conn);

    private void load_input_table()
    {
        String sql = "select * from input_metadata";
        SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
        DataTable table = new DataTable();
        adapter.Fill(table);
        BindingSource b = new BindingSource();
        b.DataSource = table;
        dataGridView1.DataSource = b;
        addcombo();

    }
    private void addcombo()
    {
        DataGridViewComboBoxColumn inputtablecombobox = new DataGridViewComboBoxColumn();
        inputtablecombobox.HeaderText = "field";
        inputtablecombobox.Name = "inputtablecombobox";
        String combosql = "select field from input_metadata";
        SqlDataAdapter comboadapter = new SqlDataAdapter(combosql, connection);
        DataSet ds = new DataSet();
        comboadapter.Fill(ds);
        inputtablecombobox.DataSource = ds.Tables[0];
        inputtablecombobox.DisplayMember = "field";
        inputtablecombobox.ValueMember = "field";
        dataGridView1.Columns.Add(inputtablecombobox);
    }
    private void load_output_table()
    {
        String sql = "select * from output_metadata";
        SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
        DataTable table = new DataTable();
        adapter.Fill(table);
        BindingSource b = new BindingSource();
        b.DataSource = table;
        dataGridView2.DataSource = b;
    }
    private void inputtablecombobox_SelectedIndexChanged(object sender, EventArgs e)
    {
          MessageBox.Show("text");
    }
}

谢谢大家!!我找到了答案

public Form1()
    {
        InitializeComponent();
        load_input_table();
        load_output_table();
        dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
    }

    static String conn = @"Data Source=SUMEET-PC\MSSQLSERVER1;Initial Catalog=EMIDS;Integrated Security=True";
    SqlConnection connection = new SqlConnection(conn);
    DataGridViewComboBoxColumn inputtablecombobox = new DataGridViewComboBoxColumn();

    private void load_input_table()
    {
        String sql = "select * from input_metadata";
        SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
        DataTable table = new DataTable();
        adapter.Fill(table);
        BindingSource b = new BindingSource();
        b.DataSource = table;
        dataGridView1.DataSource = b;
        addcombo();

    }

    private void addcombo()
    {
        inputtablecombobox.HeaderText = "field";
        inputtablecombobox.Name = "inputtablecombobox";
        String combosql = "select field from input_metadata";
        SqlDataAdapter comboadapter = new SqlDataAdapter(combosql, connection);
        DataSet ds = new DataSet();
        comboadapter.Fill(ds);
        inputtablecombobox.DataSource = ds.Tables[0];
        inputtablecombobox.DisplayMember = "field";
        inputtablecombobox.ValueMember = "field";
        dataGridView1.Columns.Add(inputtablecombobox);
    }

    private void load_output_table()
    {
        String sql = "select * from output_metadata";
        SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
        DataTable table = new DataTable();
        adapter.Fill(table);
        BindingSource b = new BindingSource();
        b.DataSource = table;
        dataGridView2.DataSource = b;
    }

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        ComboBox combo = e.Control as ComboBox;
        if (combo != null)
        {
            combo.SelectedIndexChanged -= new EventHandler(inputtablecombobox_SelectedIndexChanged);
            combo.SelectedIndexChanged += new EventHandler(inputtablecombobox_SelectedIndexChanged);
        }
    }
    private void inputtablecombobox_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show(text);
    }
}

请随时向我们展示您当前正在做什么?而不是“combobox.selectedindexchanged”`您必须收听例如DGV.CellValueChanged或其他一些DGV事件。@jomsk1e我已将我正在做的事情的屏幕截图。.当我更改combocell时,特定的行应该更改我指的是您正在使用的代码,如果您有,请向我们展示..@jomsk1e我已添加代码InputableComboBox\u SelectedIndexChanged在我更改combobox时不会触发