[英]How can I search for an exact value in a cell of the DataGridView?
我有一个名为dataGridView
的DataGridView
实例,其中包含一些行。 现在,我想在dataGridView
搜索单元格的确切值,并使用该特定行从另一个单元格获取单元格数据,并使用它的值。
在我的dataGridView中,我有4行: Name
, Filename
, Path
, HotKey
。
当我按下组合键时,它必须与我的单元格中已注册的HotKey
组合完全匹配。
例如: HotKey == F1
,这意味着当我按下F1时 ,它将使用Path
单元格来输入项目中需要的字符串。
问题:当我按F1时 ,效果很好。 但是,当我按CTRL + F1 , ALT + F1或SHIFT + F1时,它还会使用来自F1的同一行的同一行的数据。当没有值CTRL+F1
, ALT+F1
或Shift+F1
单元格时,它不应执行任何操作。 我究竟做错了什么?
这是我使用的代码:
public string typedChars = string.Empty;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string CTRL = (e.Control ? "CTRL + " : "");
string ALT = (e.Alt ? "ALT + " : "");
string SHIFT = (e.Shift ? "Shift + " : "");
string KEYPRESSED = e.KeyCode.ToString();
typedChars = $"{CTRL}{ALT}{SHIFT}{KEYPRESSED}"; // Puts all pressed key combination to a string for the HotKey combination
for (int i = 0; i < (dataGridView1.Rows.Count); i++) //counting all the rows
{
if (dataGridView1.Rows[i].Cells[3].Value.ToString().Equals(typedChars)) // search for an exact value in cell 3 and find that row (HotKey)
{
String Path = dataGridView1.Rows[i].Cells[2].Value.ToString(); //use from that founded row the cell 2 value (Path)
//do something here with the value
}
}
}
没关系,这个脚本很好。 同时还从脚本的其他地方调用了它。 因此,我对那个Sellect行有一个模糊的HotKey调用。 一个是F1 ,另一个是Shift + F1 ,看起来好像F1也在做某事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.