将Combobox添加到已包含在有界数据网格视图中的列

时间:2016-11-23 16:04:46

标签: c# datagridview combobox contextmenustrip

我希望有一个数据网格视图,该数据网格视图与SQL数据源绑定,该数据源具有名称状态的列,我还需要将此列作为组合框,以便用户更好地选择值而不是编写它,是吗有可能? 我已经搜索过了,我发现了这个Adding bound combobox to datagridview,它回答了如何将comobobox添加到有界数据网格视图中,但这不是我需要的 我也试图通过使用menustrip以不同的方式制作另一种解决方案并且它工作正常,但是在用户选择menusttrip项目之后我无法编辑单元格值,因为datagridview被绑定到数据源

以下是与第二个解决方案相关的代码的一部分:

   dtSamples = cMaster.Select_Samples(nKeyNo, DateTime.Today, strMachine);

   dataGridView_Samples.DataSource = dtSamples;


    //here is the event that shows the menustrip when the user clicked on the desired column 
    private void dataGridView_Samples_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)                                
        {
            DataGridView.HitTestInfo hit = dataGridView_Samples.HitTest(e.X, e.Y);     //Get the clicked cell
            if (e.RowIndex < nNewRowIndex)                                            //If it's a header, ignore
                return;
            dataGridView_Samples.CurrentCell = dataGridView_Samples[e.ColumnIndex, e.RowIndex];    //Select the cell for future info
            StatusRowIndex = e.RowIndex;
            if (dataGridView_Samples.CurrentCell.ColumnIndex == 4)                     //If this is the priority column
            {
                contextMenuStrip_Status.Show(Cursor.Position.X, Cursor.Position.Y); //Show the strip
            }
        }

    }

  //here is the event of selecting item of menustrip and it doesn't show the value in the cell
     private void toolStripMenuItem_Repair_Click(object sender, EventArgs e)
    {
        if (nNewRowIndex == -1)
        {
            dataGridView_Samples.CurrentCell.Value = "Repair";

            dataGridView_Samples.Rows[StatusRowIndex].Cells[4].Value = 4;
            dataGridView_Samples.NotifyCurrentCellDirty(true);
        }

        else
        {
            dataGridView_Samples.Rows[nNewRowIndex].Cells[4].Value = "Repair";
           // dataGridView_Samples.Rows[nNewRowIndex].Cells[4].Value = 4;
            dataGridView_Samples.NotifyCurrentCellDirty(true);
            dataGridView_Samples.BeginEdit(true);
            dataGridView_Samples.EndEdit();
        }

    }

0 个答案:

没有答案