如何在dataGridViewComboBoxCell中呈现多个控件类型 [英] how to render multiple control types in dataGridViewComboBoxCell

查看:91
本文介绍了如何在dataGridViewComboBoxCell中呈现多个控件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个C#winForm,它使用一个dataGridView,它有一个包含多个DataGridViewCell类型的Column。例如



I''m working on a C# winForm that uses a dataGridView that has a Column with multiple DataGridViewCell types. e.g.

DataGridView dvg = new DataGridView();
dgv.Columns.Add("colCtrl", "Ctrl");

DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();

TextBox txtBox = new TextBox();
txtBox.Name = "MyTextBox";
txtBox.KeyDown += txtBox_KeyDown;

cboCell.Items.Add(txtBox);

DataGridViewRow row = new DataGridViewRow();
row.Cells.Add(cboCell);

dgv.Rows.Add(row);

private void txtBox_KeyDown(object sender, KeyEventArgs e)
{
   DataGridViewComboBoxCell cboCell= (DataGridViewComboBoxCell)((TextBox)sender).Parent;
   TextBox txtBox = (TextBox)sender;

   if (e.KeyCode == Keys.Enter)
   {
       if (!cb.Items.Contains(txtBox.Text))
           cb.Items.Add(txtBox.Text);
   }
}





textBox控件未呈现给comboBox Items集合。它似乎是在我单步执行代码时添加但它没有出现。我基本上想使用textBox将字符串项添加到我的comboBox dropDownList中。我在这里做错了什么?



提前致谢,

-DA



the textBox control is not being render to the comboBox Items collection. Its seems to be added when I step through code but It''s not showing up. I''d basically like to use the textBox to add string items to the my comboBox dropDownList. What am I doing wrong here?

Thanks in advance,
-DA

推荐答案

您必须创建自己的DataGridViewColumn才能执行此类操作。这不容易,而且你是对的,DGV API有点复杂。这就是为什么有经验的人只使用它来显示数据,而不是编辑它。
You''d have to create your own DataGridViewColumn to do something like this. It''s not easy, and you''re correct, the DGV API is a bit convoluted. That''s why anyone with any experience with it only uses it to display data, not edit it.


我要说的只是MSFT需要回到绘图板并重新修改的dataGridView。不要误解我它有很多很好的功能,但它的方法很复杂,缺乏你期望API支持的常用功能!似乎每次我使用dataGridView我都必须通过箍来完成最简单的任务,但无论如何这里是我的黑客工作来渲染一个文本框...请记住,这是一个HACK远远不是这样做的正确方法,哦......希望这有助于某人。



All I have to say is MSFT needs go back to the drawing board and revamp the dataGridView. Don''t get me wrong it has a lot good functionality but its WAY to convoluted and lacks common features that you would expect the API to support! Seems like every time I work with the dataGridView I have to jump through hoops to accomplish the most simple of task, but anyway here''s my hack job to render a textbox...keep in mind this is a HACK far from being the correct way to do this, oh well... hope this helps someone.

public class DropDownCellWithTextBox : DataGridViewComboBoxCell
{
   ContextMenuStrip dropDownList;
   ToolStripTextBox txtBox;
   DataGridView dgv;

   public DropDownCellWithTextBox(DataGridView _dgv)
   {
       dgv = _dgv;

       dropDownList = new ContextMenuStrip();

       txtBox = new ToolStripTextBox();
       txtBox.BorderStyle = BorderStyle.FixedSingle;
       txtBox.KeyDown += txtBox_KeyDown;
       
       dropDownList.Items.Add(txtBox);
   }

   public override void InitializeEditingControl(int rowIndex,
   object initialFormattedVaulue, DataGridViewCellStyle dataGridViewCellStyle)
   {
       base.InitializeEditingControl(rowIndex,initialFormattedValue,dataGridViewCellStyle);

            ContextMenuStrip cms = DataGridView.EditingControl as ContextMenuStrip;
   }

    protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
    {
        //base.OnMouseClick(e)
        dropDownList.Size = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Size;
        dropDownList.Show(dgv.PointToScreen(new Point(e.X, (e.Y + 49))));
    }
    
    private void txtBx_KeyDown(object sender, KeyEventArgs e)
    {
        ToolStripTextBox txt = (ToolStripTextBox)sender;
        if (txt.Text == "")
            return;
           
        if (e.KeyCode == Keys.Enter)
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem(txt.Text);
            tsmi.Name = txt.Text;

            if (!dropDownList.Items.ContainsKey(tsmi.Name))
            {
                 dropDownList.Items.Add(tsmi);
                 txt.Text = "";
            }
        }
    }
}


这篇关于如何在dataGridViewComboBoxCell中呈现多个控件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆