我正在为DataGridView
的特定列中的所有单元格制作自动填充行为。
我一直在寻找一种方法这样做几个小时然而,我找不到任何有用的东西。
有许多事件,例如当用户点击其中一个时,DataGridView
中的用户键(被调用方式太多次)等等,但是Cell内没有KeyUp事件。
有没有办法做到这一点?
非常感谢。
答案 0 :(得分:0)
您可以尝试在该列中使用模板字段和文本框,然后可以使用该文本框吗?
答案 1 :(得分:0)
您可以使用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;
}
}
}
答案 2 :(得分: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