使用文本框搜索DataGrid
Use Textbox to search DataGrid
我已经解决这个问题几个小时了,但似乎无法正常工作。
好像不是在搜索行?
private void searchbutton_Click(object sender, EventArgs e)
{
string searchValue = searchtextBox.Text;
int rowIndex = 0;
inkGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
bool valueResulet = true;
foreach (DataGridViewRow row in inkGridView.Rows)
{
if (row.Cells[rowIndex].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
inkGridView.Rows[rowIndex].Selected = true;
rowIndex++;
valueResulet = false;
}
}
if (valueResulet != false)
{
MessageBox.Show("Unable to find "+ searchtextBox.Text,"Not Found");
return;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
它只是总是抛出错误。
在我看来,问题出在您的 if 语句上。即 row.Cells[rowIndex]
returns 每行中的单元格,因此您命名为 rowIndex 并在 if 语句中使用的实际上是列索引。因此,您可以将其更改为:
if (row.Cells[0].Value.ToString().Equals(searchValue))
搜索第一列。要检查所有单元格列,您必须在每一行中遍历它们。
您可能还想全面了解一下 rowIndex 的使用情况,因为您首先将其分配给 0 并在循环中增加它。但是,然后您将其分配给
rowIndex = row.Index;
并继续递增它(但前提是找到匹配项)。我认为您只是混合了行和列索引 up/intertwined。
编辑:我认为抛出的异常是由于当您遍历单元格 (0, 0)、(1, 1)、(2, 2) ... 等等时,没有足够的列循环遍历指定列的行。
我为我使用的 dataGridView 创建了一个搜索文本框,所以也许这对您有用,因为控件非常相似。虽然我选择了找不到时不给消息。它不是一条消息,而是将文本框变成红色,它还试图找到完整文本的第一次出现。如果找不到完全匹配项,它将尝试查找包含搜索值
的匹配项
private void searchart()
{
int itemrow = -1;
String searchValue = cueTextBox1.Text.ToUpper();
if (searchValue != null && searchValue != "")
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//search for identical art
if (row.Cells[0].Value.ToString().Equals(searchValue))
{
itemrow = row.Index;
break;//stop searching if it's found
}
//search for first art that contains search value
else if (row.Cells[0].Value.ToString().Contains(searchValue) && itemrow == -1)
{
itemrow = row.Index;
}
}
//if nothing found set color red
if (itemrow == -1)
{
cueTextBox1.BackColor = Color.Red;
}
//if found set color white, select the row and go to the row
else
{
cueTextBox1.BackColor = Color.White;
dataGridView1.Rows[itemrow].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = itemrow;
}
}
}
我会说,除非您将 DataGridView.AllowUserToAddRows
设置为 false
,否则一旦您尝试访问最后一行的 Value.ToString()
,就会出现空引用异常。因此,要么将其设置为 false
,要么如果你想允许添加新行,只需添加一个检查
if (row.IsNewRow) continue;
我已经解决这个问题几个小时了,但似乎无法正常工作。
好像不是在搜索行?
private void searchbutton_Click(object sender, EventArgs e)
{
string searchValue = searchtextBox.Text;
int rowIndex = 0;
inkGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
bool valueResulet = true;
foreach (DataGridViewRow row in inkGridView.Rows)
{
if (row.Cells[rowIndex].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
inkGridView.Rows[rowIndex].Selected = true;
rowIndex++;
valueResulet = false;
}
}
if (valueResulet != false)
{
MessageBox.Show("Unable to find "+ searchtextBox.Text,"Not Found");
return;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
它只是总是抛出错误。
在我看来,问题出在您的 if 语句上。即 row.Cells[rowIndex]
returns 每行中的单元格,因此您命名为 rowIndex 并在 if 语句中使用的实际上是列索引。因此,您可以将其更改为:
if (row.Cells[0].Value.ToString().Equals(searchValue))
搜索第一列。要检查所有单元格列,您必须在每一行中遍历它们。
您可能还想全面了解一下 rowIndex 的使用情况,因为您首先将其分配给 0 并在循环中增加它。但是,然后您将其分配给
rowIndex = row.Index;
并继续递增它(但前提是找到匹配项)。我认为您只是混合了行和列索引 up/intertwined。
编辑:我认为抛出的异常是由于当您遍历单元格 (0, 0)、(1, 1)、(2, 2) ... 等等时,没有足够的列循环遍历指定列的行。
我为我使用的 dataGridView 创建了一个搜索文本框,所以也许这对您有用,因为控件非常相似。虽然我选择了找不到时不给消息。它不是一条消息,而是将文本框变成红色,它还试图找到完整文本的第一次出现。如果找不到完全匹配项,它将尝试查找包含搜索值
的匹配项private void searchart()
{
int itemrow = -1;
String searchValue = cueTextBox1.Text.ToUpper();
if (searchValue != null && searchValue != "")
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//search for identical art
if (row.Cells[0].Value.ToString().Equals(searchValue))
{
itemrow = row.Index;
break;//stop searching if it's found
}
//search for first art that contains search value
else if (row.Cells[0].Value.ToString().Contains(searchValue) && itemrow == -1)
{
itemrow = row.Index;
}
}
//if nothing found set color red
if (itemrow == -1)
{
cueTextBox1.BackColor = Color.Red;
}
//if found set color white, select the row and go to the row
else
{
cueTextBox1.BackColor = Color.White;
dataGridView1.Rows[itemrow].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = itemrow;
}
}
}
我会说,除非您将 DataGridView.AllowUserToAddRows
设置为 false
,否则一旦您尝试访问最后一行的 Value.ToString()
,就会出现空引用异常。因此,要么将其设置为 false
,要么如果你想允许添加新行,只需添加一个检查
if (row.IsNewRow) continue;