开发者

Catching a KeyUp event inside a DataGridViewCell

I'm making an autocomplete behavior for all cells from a specific column of my DataGridView.

I've been searching for a way of doing so for hours and yet, I did not find anything useful.

There's many events such as when a user click in one of them, when a user keyup in t开发者_Go百科he DataGridView (which gets called way too many times), etc. But no KeyUp event inside a Cell.

Is there any way of doing it?

Thanks a lot.


You could try and use a template field and a textbox inside that column which you can then use keyup on that?


You can use a datagridview textbox column and set its autocomplete source

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

Something like this,

 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;
                }
            }
        }


This works for me (for a keypress, same should be true for a key up / down):

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜