2011-05-02 86 views
0

我对来自我的DataGridView的特定列的所有单元格进行自动填充行为。在DataGridViewCell中捕获KeyUp事件

我一直在寻找一种这样做的方式几个小时,但是,我没有找到任何有用的东西。

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

有没有办法做到这一点?

非常感谢。

回答

0

您可以尝试在该列内使用模板字段和文本框,然后您可以使用该字段和关键字?

+0

有没有更简单的方法,这??这已经是一个DataGridViewTextBoxColumn应该有一个文本框,它应该是“事件绑定”不是?如果没有,那么你是否有任何关于你所谈论的文件?谢谢 – TomShreds 2011-05-02 18:59:06

+0

啊,我主要使用gridview,所以我没有离开“DataGridViewTextBoxColumn”Priyank的答案看起来好多了。 – 2011-05-02 19:15:38

0

您可以使用一个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; 
       } 
      } 
     } 
0

这对我的作品(每次按键,同样应该是真实的一键上/下):

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