繁体   English   中英

在datagridview中为特定单元格添加下拉列表

[英]Add dropdown for a specific cell in a datagridview

我试图将不同的控件添加到同一列中的单元格。 下拉列表不显示,并且没有可见的设置器:

private void AddBooleanDropDown(DataGridView grid, int row, KeyValuePair<string, string> kvp)
{
    DataGridViewComboBoxCell dropDownCell = new DataGridViewComboBoxCell();
    dropDownCell.DataSource = new string[] { "True", "False" };

    grid.Rows[row].Cells["Value"] = dropDownCell;

}

不知道这是否对您有帮助,但也许是替代方法?

我希望能够更新读入DataGridView的Excel电子表格,并为用户提供一些选择。 我使用了将在MouseClick事件上显示的ContextMenuStrip。

当您右键单击单元格时,它将显示一个小菜单:

优先级下拉列表

如果不是您要找的东西,对不起; 也许只是一个替代解决方案:

        ////////////////////////////////////////////////////////////////////////
        //Change Priority Strip
        ////////////////////////////////////////////////////////////////////////
        ContextMenuStrip changePriority = new ContextMenuStrip();
        ToolStripMenuItem highPriority = new ToolStripMenuItem("High Priority");
        changePriority.Items.Add(highPriority);
        highPriority.Click += new EventHandler(changePriorityHighEvent);
        ToolStripMenuItem normalPriority = new ToolStripMenuItem("Normal Priority");
        changePriority.Items.Add(normalPriority);
        normalPriority.Click += new EventHandler(changePriorityNormalEvent);
        ToolStripMenuItem lowPriority = new ToolStripMenuItem("Low Priority");
        changePriority.Items.Add(lowPriority);
        lowPriority.Click += new EventHandler(changePriorityLowEvent);
        ////////////////////////////////////////////////////////////////////////

        private void gridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)                                //On Right Click
            {
                DataGridView.HitTestInfo hit = gridView.HitTest(e.X, e.Y);     //Get the clicked cell
                if (e.RowIndex < 0)                                            //If it's a header, ignore
                    return;
                gridView.CurrentCell = gridView[e.ColumnIndex, e.RowIndex];    //Select the cell for future info
                if (gridView.CurrentCell.ColumnIndex == 6)                     //If this is the priority column
                {
                    changePriority.Show(Cursor.Position.X, Cursor.Position.Y); //Show the strip
                }
             }
         }

         private void changePriorityHighEvent(Object sender, EventArgs e) {
             //make changes
         }
         private void changePriorityNormalEvent(Object sender, EventArgs e) {
             //make changes
         }
         private void changePriorityLowEvent(Object sender, EventArgs e) {
             //make changes
         }

这是非常好的MSDN示例

DataGridView控件提供了几种列类型,使您的用户可以通过多种方式输入和编辑值。 但是,如果这些列类型不满足您的数据输入需求,则可以使用包含您选择的控件的单元格来创建自己的列类型。 为此,必须定义派生自DataGridViewColumnDataGridViewCell的类 您还必须定义一个从Control派生并实现IDataGridViewEditingControl接口的类。

不知道是否可以更改网格中的特定单元格,除非它是同一类型。 您可以尝试使用该数据源添加一个新的组合框列

var newCol = new DataGridViewComboBoxColumn()
{
       DataSource = new string[] { "True", "False" }
};

grid.Columns.Add(newCol);

您也可能要检查传入的int不大于行数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM