【问题标题】:Catching a KeyUp event inside a DataGridViewCell在 DataGridViewCell 中捕获 KeyUp 事件
【发布时间】:2011-05-02 18:46:41
【问题描述】:

我正在为我的DataGridView 的特定列中的所有单元格进行自动完成行为。

我已经寻找了几个小时的方法,但是我没有找到任何有用的东西。

有许多事件,例如当用户点击其中一个时,当用户在DataGridView 中键入(被调用的次数太多)等等。但在 Cell 中没有 KeyUp 事件。

有什么办法吗?

非常感谢。

【问题讨论】:

    标签: c# .net datagridview


    【解决方案1】:

    您可以尝试在该列中使用模板字段和文本框,然后您可以对其使用 keyup 吗?

    【讨论】:

    • 没有比这更简单的方法了吗?!这已经是一个 DataGridViewTextBoxColumn 应该有一个文本框,它应该是“事件绑定”不是吗?如果没有,那么您是否有任何有关您所谈论内容的文件?谢谢
    • 啊,我主要使用网格视图,所以我并没有离开“DataGridViewTextBoxColumn” Priyank 的答案看起来更好。
    【解决方案2】:

    您可以使用 datagridview 文本框列并设置其自动完成源

    http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/276e8f89-5cef-4208-a4be-08f4000bd753/

    类似的,

     AutoCompleteStringCollection scAutoComplete = new AutoCompleteStringCollection();
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                String strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;";
                SqlConnection conn = new SqlConnection(strConn);
                SqlDataAdapter da = new SqlDataAdapter("Select * from [Order Details]", conn);
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                for (int x = 1; x <= 61000; x++ )
                {
                    scAutoComplete.Add(x.ToString());
                }
            }
            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (dataGridView1.CurrentCellAddress.X == 1)
                {
                    TextBox txt = e.Control as TextBox;
                    txt.AutoCompleteCustomSource = scAutoComplete;
                    txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }
            }
            private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
            {
                if(e.ColumnIndex==1)
                {
                    if(!scAutoComplete.Contains(e.FormattedValue.ToString()))
                    {
                        MessageBox.Show("Invalid Data");
                        e.Cancel=true;
                    }
                }
            }
    

    【讨论】:

      【解决方案3】:

      这对我有用(对于按键,向上/向下键也应该如此):

      Private dgTextbox As TextBox = New TextBox
      
      Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
      
          RemoveHandler dgTextbox.KeyPress, AddressOf dgTextbox_KeyPress
          dgTextbox = CType(e.Control, TextBox)
          AddHandler dgTextbox.KeyPress, AddressOf dgTextbox_KeyPress
      
      End Sub
      
      Private Sub dgTextbox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
          'your code goes here
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多