【发布时间】:2017-05-03 16:23:45
【问题描述】:
我需要修剪 textbox2_KeyPress 事件中的 datagridview 单元格,以便它在 textbox2 中的 datagrid 中找到匹配值之前按键在 datagrid 中搜索 textbox2 的字符串。
目前 CellFormatting 事件发生在 KeyPress 查找值之后。
我的代码;
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();
}
}
【问题讨论】:
-
尽管它们的名称不同,但它仍然有效。 CellFormatting 修剪 DataGrid 中的所有字符串。但是,当我需要它在正则表达式运行之后但在它找到匹配值之前执行它时,它会在 KeyPress 之后执行它
-
DataGrid 是只读的。因为它显示了构成 KIT 产品的各种子产品,用户将使用条形码扫描仪对其进行扫描。他们不会编辑单元格。我遇到的问题是包含扫描到 textbox2 的部件号的单元格中的字符串后面有空格,因此代码找不到匹配项,因为 textbox2 后面没有空格。
-
啊,是的,我现在明白了!抱歉,对此是新的;)...在这种情况下,尽管我可以看到有匹配项,但我仍然遇到在数据网格中找不到匹配值的问题..
标签: c# datagridview datagrid