Reputation: 615
I have created a simple DataGridView
through the toolbox and have selected data through the wizard (no code in .cs file) from a database. It is working flawlessly as you can see in the picture below:
Now I want to filter the entries in it by contact person name. I have a textbox and search button so when the user enters a "contact person name" such as "Altaf" and then clicks on search, the GridView
should refresh and only entries with ticketid=4
should appear.
The only code in the .cs file (which is auto-generated) is:
private void Form2_Load(object sender, EventArgs e)
{
this.tblTicketDetailTableAdapter.Fill(this.sTDataSet1.tblTicketDetail); //auto-generated
}
I tried this in a ButtonClick
event as suggested by someone but it generates the error: "Cannot interpret token '{' at position 27"
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = issuerNameDataGridViewTextBoxColumn + "like '%" + txtbxSearch.Text.Trim().Replace("'", "''") + "%'";
dataGridView1.DataSource = bs.DataSource;
I have no experience in DataGridViews
or WinForms coding, so please explain in detail.
Upvotes: 7
Views: 50615
Reputation: 1532
You can use the dataGridView1.Rows[iIndex].Visible
in order to filter a dataGridView in case you don't have a datasource. Whereas this may be very slow if the AutoSizeMode of the column widths is enabled. (At least I had this problem).
The solution was to turn the AutoSizeMode
off temporarily, then filtering this way is quite fast:
for (int z = 0; z < dataGridView1.Columns.Count; z++)
{ // Disabled AutoSize Mode for all columns
dataGridView1.Columns[iRow].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
}
for (int iRow = 0; iRow <= dataGridView1.RowCount; iRow++)
{ // Filtering dataGridView1
bool bVisibleCondition = ...
dataGridView1.Rows[iRealRow].Visible = bVisibleCondition;
}
for (int z = 0; z < dataGridView1.Columns.Count; z++)
{ // Enable AutoSize Mode for all columns
dataGridView1.Columns[z].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
Upvotes: 1
Reputation: 615
Thank you everyone that has answered to my query, I really appreciate your help guys. You guys are the most helpful bunch.
I have solved my problem by doing following modifications to my code :
public void btnSearch_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = dataGridView1.Columns[5].HeaderText.ToString() + " LIKE '%" + txtbxSearch.Text + "%'";
dataGridView1.DataSource = bs;
}
Thank you again.
Upvotes: 15
Reputation: 18737
Try this:
foreach (System.Windows.Forms.DataGridViewRow r in MyGridView.Rows)
{
if ((r.Cells[5].Value).ToString().ToUpper().Contains(searchText.ToUpper()))
{
MyGridView.Rows[r.Index].Visible = true;
MyGridView.Rows[r.Index].Selected = true;
}
else
{
MyGridView.CurrentCell = null;
MyGridView.Rows[r.Index].Visible = false;
}
}
Upvotes: 8
Reputation: 2974
I hope I got your problem well
string whereClause = "ContactPerson=" +textbox.text;
(datagridview.DataSource as DataTable).DefaultView.RowFilter = whereClause;
Upvotes: 2