在datagridview中加载表格后,我会从datagrid列标题生成组合框的项目。从组合框我选择列,我有一个用户给出的搜索值的文本框。我使用以下代码:
string searchForText = txtCrudSearch.Text;
dgvLoadTable.ClearSelection();
dgvLoadTable.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dgvLoadTable.Rows)
{
if (row.Cells[cboCrudSearchColumn.SelectedIndex].Value.ToString().Equals(searchForText))
{
row.Selected = true;
//if I use break here the code doesn't give exception
//but highlights only one row. I want to return all rows that
// match with the search string
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
现在,问题是:
任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
我明白了。如果有人需要解决方案,代码如下:
private void btnCrudSearch_Click(object sender, EventArgs e)
{
dgvLoadTable.CurrentCell = null;
dgvLoadTable.AllowUserToAddRows = false;
dgvLoadTable.ClearSelection();
dgvLoadTable.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgvLoadTable.ReadOnly = true;
try
{
foreach (DataGridViewRow row in dgvLoadTable.Rows)
{
var cellValue = row.Cells[cboCrudSearchColumn.SelectedIndex].Value;
if (cellValue != null && cellValue.ToString() == txtCrudSearch.Text.ToUpper())
{
row.Selected = true;
row.Visible = true;
}
else
row.Visible = false;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}