我对来自我的DataGridView
的特定列的所有单元格进行自动填充行为。在DataGridViewCell中捕获KeyUp事件
我一直在寻找一种这样做的方式几个小时,但是,我没有找到任何有用的东西。
有很多事件,例如当用户点击其中的一个,当用户键入DataGridView
(它被调用的方式太多次)等时,但Cell内没有KeyUp事件。
有没有办法做到这一点?
非常感谢。
我对来自我的DataGridView
的特定列的所有单元格进行自动填充行为。在DataGridViewCell中捕获KeyUp事件
我一直在寻找一种这样做的方式几个小时,但是,我没有找到任何有用的东西。
有很多事件,例如当用户点击其中的一个,当用户键入DataGridView
(它被调用的方式太多次)等时,但Cell内没有KeyUp事件。
有没有办法做到这一点?
非常感谢。
您可以尝试在该列内使用模板字段和文本框,然后您可以使用该字段和关键字?
您可以使用一个DataGridView文本框栏,并设置其自动完成源
这样的事情,
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;
}
}
}
这对我的作品(每次按键,同样应该是真实的一键上/下):
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
有没有更简单的方法,这??这已经是一个DataGridViewTextBoxColumn应该有一个文本框,它应该是“事件绑定”不是?如果没有,那么你是否有任何关于你所谈论的文件?谢谢 – TomShreds 2011-05-02 18:59:06
啊,我主要使用gridview,所以我没有离开“DataGridViewTextBoxColumn”Priyank的答案看起来好多了。 – 2011-05-02 19:15:38