0

I've tried to make a filter for DataGridView which would show me the row that selected in combobox, but it didn't work out. What should I change in my code?

DataView dv = new DataView(dt);
dv.RowFilter = string.Format("Convert({0}, System.String) LIKE '%{1}%'", dataGridView1.Rows[0].Index, comboBox1.SelectedItem);
dataGridView1.DataSource = dv;

I expected to see row, but I've got empty DataGridView. Thanks.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • There must be [quotes around `System.String`](https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?view=netframework-4.8#functions), and the first argument to `Convert` is a column name, not a row index. – GSerg May 19 '19 at 20:53
  • @GSerg yes, it works with columns, but how may I get this thing work with index? I need to filter row number, not column value and I can't change database to add new column with numbers. Thanks for the quotes btw. – Daniel Dashkevich May 19 '19 at 21:06
  • Possible duplicate of [Hide a row in DataGridView](https://stackoverflow.com/questions/19855155/hide-a-row-in-datagridview) – GSerg May 19 '19 at 21:08
  • @GSerg not exactly my problem. I need filter it with combobox value, not just hide selected rows. – Daniel Dashkevich May 19 '19 at 21:33
  • It shows how to hide specific rows in a datagrid based on index as opposed to column values. The only relevant part is `Rows[row.Index].Visible = false`. – GSerg May 19 '19 at 21:41
  • @GSerg So you suggest me to hide rows that not equal to combobox value by using loop, am I right? – Daniel Dashkevich May 19 '19 at 21:51
  • You are right.. – GSerg May 19 '19 at 21:54
  • @GSerg Now it gives me index out of range – Daniel Dashkevich May 19 '19 at 22:40

0 Answers0