在vb.net中,如何将选定的DataGridViewComboBoxColumn值映射到DataGridViewTextBoxColumn

时间:2016-06-20 07:28:03

标签: c# vb.net

enter image description here

查看图片

部门ID为 DataGridViewComboBoxColumn
部门名称为 DataGridViewTextBoxColumn

当我选择部门ID时,该ID应显示在textbox.later上我可以将id映射到部门名称。现在问题是我不知道如何获得所选的 DataGridViewComboBoxColumn值并分配给 DataGridViewTextBoxColumn

2 个答案:

答案 0 :(得分:0)

假设您有一个数据表作为源。如果是,则在datagrid视图的click事件中获取所选值

Dim strValue as String = dgv.Rows(e.RowIndex).Cells(DataGridViewComboBoxColumn).Value

现在遍历您的数据表并获取具有所选ID的相应部门并获取值以将其放置在文本框列上。像这样的东西

Dim row AS DataRow = DataTable.Select("ComboboxColumn = strValue").FirstOrDefault()
If Not row Is Nothing Then strTextBoxColumn = row.Item("ComboboxColumn")

现在将值分配给datagrid视图

上的文本框列
dgv.rows(e.rowIndex).Cells(DataGridViewComboBoxColumn).Value = strTextBoxColumn

答案 1 :(得分:0)

此问题的

解决方案

  

我的完整代码位于

之下

表单加载模块

private void Form1_Load(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.Add("001");
        bs.Add("002");
        bs.Add("003");
        bs.Add("004");
        bs.Add("005");
        try
        {
           DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
            combo1.HeaderText = "DepartmentID";
            combo1.DataSource = bs;
            combo1.Width = 220;
            dataGridView1.Columns.Add(combo1);

                DataGridViewTextBoxColumn text1 = new DataGridViewTextBoxColumn();
                text1.Width = 200;
                text1.HeaderText="DEPARTMENTNAME";
                text1.ReadOnly = true;  
                DataGridViewTextBoxCell txt = new DataGridViewTextBoxCell();
                dataGridView1.Columns.Add(text1);

                DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
                combo2.HeaderText = "Status";
                combo2.Items.Add("Active");
                combo2.Items.Add("InActive");
                combo2.Width = 220;
                dataGridView1.Columns.Add(combo2);
        }
        catch (Exception ex)
        {
            MessageBox.Show("combo error");
        }


    }

dataGridView1_EditingControlShowing Module

    private void dataGridView1_EditingControlShowing_1(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        ComboBox combo = e.Control as ComboBox;
        if (combo != null)
        {
            // Remove an existing event-handler, if present, to avoid 
            // adding multiple handlers when the editing control is reused.
            combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);

            // Add the event handler. 
            combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
        }
    }

ComboBox_SelectedIndexChanged模块

    private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox cb = (ComboBox)sender;
        string item = cb.Text;
        int irow = dataGridView1.CurrentCell.RowIndex;

        if (item != null)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 0) // This IF is used for handling first combobox event in gridview
            {
                dataGridView1.Rows[irow].Cells[1].Value = item; // cell index [1] represent DepartmentName
            }

        }

    }

输出

Click here to view Output