我希望用户能够在DataGridView(dgv)的列中搜索数字。 dgv可以保存很多记录。每条记录都有一个项目编号。所以我希望用户能够在项目编号列中搜索项目编号。我的列是:ProjectID(不可见);图像(无headertext);项目编号;项目名;公司;接触。
这是我的代码:
private void btnSearch_Click(object sender, EventArgs e)
{
string searchValue = textBox1.Text;
int rowIndex = -1;
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dgvProjects.Rows)
{
if (row.Cells[row.Index].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
dgvProjects.Rows[row.Index].Selected = true;
break;
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
问题#1:到目前为止:用户在TextBox1中键入项目编号。当他/她单击按钮时,代码会在行中搜索此字符串,当找到项目编号时,该行将被选中。它工作正常,但只有一次。当我想搜索其他项目编号时,没有任何反应。
问题#2:我认为只需搜索“项目名称”列的值,就可以更好地完成此操作。但是我该怎么做呢?
我用于搜索的代码来自this answer
答案 0 :(得分:18)
为什么使用row.Cells [row.Index]。您需要指定要搜索的列的索引(问题#2)。例如,您需要将row.Cells [row.Index]更改为row.Cells [2],其中2是列的索引:
private void btnSearch_Click(object sender, EventArgs e)
{
string searchValue = textBox1.Text;
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dgvProjects.Rows)
{
if (row.Cells[2].Value.ToString().Equals(searchValue))
{
row.Selected = true;
break;
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
答案 1 :(得分:1)
最好在另一种方法中分离你的逻辑,或者在另一种方法中分离你的逻辑。
此方法将帮助您检索找到文本的DataGridViewCell对象。
/// <summary>
/// Check if a given text exists in the given DataGridView at a given column index
/// </summary>
/// <param name="searchText"></param>
/// <param name="dataGridView"></param>
/// <param name="columnIndex"></param>
/// <returns>The cell in which the searchText was found</returns>
private DataGridViewCell GetCellWhereTextExistsInGridView(string searchText, DataGridView dataGridView, int columnIndex)
{
DataGridViewCell cellWhereTextIsMet = null;
// For every row in the grid (obviously)
foreach (DataGridViewRow row in dataGridView.Rows)
{
// I did not test this case, but cell.Value is an object, and objects can be null
// So check if the cell is null before using .ToString()
if (row.Cells[columnIndex].Value != null && searchText == row.Cells[columnIndex].Value.ToString())
{
// the searchText is equals to the text in this cell.
cellWhereTextIsMet = row.Cells[columnIndex];
break;
}
}
return cellWhereTextIsMet;
}
private void button_click(object sender, EventArgs e)
{
DataGridViewCell cell = GetCellWhereTextExistsInGridView(textBox1.Text, myGridView, 2);
if (cell != null)
{
// Value exists in the grid
// you can do extra stuff on the cell
cell.Style = new DataGridViewCellStyle { ForeColor = Color.Red };
}
else
{
// Value does not exist in the grid
}
}
答案 2 :(得分:0)
// This is the exact code for search facility in datagridview.
private void buttonSearch_Click(object sender, EventArgs e)
{
string searchValue=textBoxSearch.Text;
int rowIndex = 1; //this one is depending on the position of cell or column
//string first_row_data=dataGridView1.Rows[0].Cells[0].Value.ToString() ;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
bool valueResulet = true;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[rowIndex].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
dataGridView1.Rows[rowIndex].Selected = true;
rowIndex++;
valueResulet = false;
}
}
if (valueResulet != false)
{
MessageBox.Show("Record is not avalable for this Name"+textBoxSearch.Text,"Not Found");
return;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
答案 3 :(得分:0)
为什么不首先构建DataTable
,然后将其DataGridView
分配给DataSource
:
DataTable table4DataSource=new DataTable();
table4DataSource.Columns.Add("col00");
table4DataSource.Columns.Add("col01");
table4DataSource.Columns.Add("col02");
...
(手动,循环或通过数据库表中的DataReader
添加行)
(分配数据源)
dtGrdViewGrid.DataSource = table4DataSource;
然后使用:
(dtGrdViewGrid.DataSource as DataTable).DefaultView.RowFilter = "col00 = '" + textBoxSearch.Text+ "'";
dtGrdViewGrid.Refresh();
您甚至可以将此段代码放在textbox_textchange
事件中,并在您编写时显示过滤后的值。
答案 4 :(得分:0)
直接从DataTable
或Dataset
:
"MyTable".DefaultView.RowFilter = "<DataTable Field> LIKE '%" + textBox1.Text + "%'";
this.dataGridView1.DataSource = "MyTable".DefaultView;
在KeyUp
的事件Textbox
上使用此代码,为您的表名或数据集替换“MyTable”,替换您想要搜索的字段。
答案 5 :(得分:0)
&#34; MyTable&#34; .DefaultView.RowFilter =&#34;喜欢&#39;%&#34; + textBox1.Text +&#34;%&#39;&#34 ;; this.dataGridView1.DataSource =&#34; MyTable&#34; .DefaultView;
与数据库连接和Datatable的关系如何?我应该如何设置DefaultView正确?
我使用此代码来获取数据:
con = new System.Data.SqlServerCe.SqlCeConnection();
con.ConnectionString = "Data Source=C:\\Users\\mhadj\\Documents\\Visual Studio 2015\\Projects\\data_base_test_2\\Sample.sdf";
con.Open();
DataTable dt = new DataTable();
adapt = new System.Data.SqlServerCe.SqlCeDataAdapter("select * from tbl_Record", con);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();