我需要修剪textbox2_KeyPress事件中的datagridview单元格,以便它在textbox2 中的数据网格中找到匹配值,然后按键在数据网格中搜索textbox2的字符串。
目前,在KeyPress查找值后,会发生CellFormatting事件。
我的代码;
private void textBox2_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
iCBOMHDataGridView.DataSource = iCBOMHBindingSource;
string input = textBox2.Text;
string output = "";
textBox2.Text = Regex.Replace(input, @"^\d{4}|[A-z]{2}[0-9]{5},|,|,|\d{|[0-9]{4}/....|\d{1,}\/\d{2,2}\/\d{4}|\s.........|\s|,|,|,|\d*?.$|[*?:/]\n|\r|\r\n", output);
foreach (DataGridViewRow row in iCBOMHDataGridView.Rows)
if ((string)row.Cells["PARTNUMBER"].Value == textBox2.Text)
{
row.Selected = true;
MessageBox.Show("Part Number Found");
}
else
{
row.Selected = false;
MessageBox.Show("Part Number Not Found");
}
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex > -1)
{
e.Value = e.Value.ToString().Trim();
}
}
答案 0 :(得分:0)
也许您可以使用KeyDown而不是KeyPress,因为之前会发生这种情况。不确定它是否足够早。
编辑:甚至是PreviewKeyDown事件。