我對來自我的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