Click here to Skip to main content
15,885,800 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am trying to filter the datagridview with combobox selectedvaluechanged. when ever i am trying to do so it gives me error. that the column not found
i binded datagridview with database programatically and when the grid is filled with data then i am trying to filter data
i am doing it in the windows form c# can you please help me...

What I have tried:

this i have tried and says column series not found.

private void cmbBillSeries_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt1 = new DataTable();
            DataView dv = dt1.DefaultView;
            dv.RowFilter = string.Format("Series  LIKE '%{0}%'", cmbBillSeries.SelectedItem.ToString());
            grdSpanSale.DataSource = dv;
        }
Posted
Updated 17-Jun-19 20:44pm

1 solution

Your code didn't filter against your datagridview datasource.
This line just creates an empty DataTable and then you filter:
C#
DataTable dt1 = new DataTable(); //empty datatable
DataView dv = dt1.DefaultView;
dv.RowFilter = string.Format("Series  LIKE '%{0}%'", cmbBillSeries.SelectedItem.ToString()); //you filter an empty database without columns - hence you got an error


You can try this to filter against datagridview datasource:
(grdSpanSale.DataSource as DataTable).DefaultView.RowFilter = string.Format("Series  LIKE '%{0}%'", cmbBillSeries.SelectedItem.ToString());
 
Share this answer
 
Comments
MukulMohal 18-Jun-19 4:14am    
when i tried using this i got the error

'Object reference not set to an instance of an object.'

(... as System.Data.DataTable) returned null.
Keviniano Gayo 18-Jun-19 5:26am    
How did you load the datasource into your datagridview?
MukulMohal 18-Jun-19 5:59am    
this is the method with which i am populating my datagridview

public void fromTOdate()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            cmd = new SqlCommand("rptSaleSearch", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@frmdt", frmdtDateTime.Text);
            cmd.Parameters.AddWithValue("@todt", todtDateTime.Text);
            cmd.ExecuteNonQuery();
            adp = new SqlDataAdapter(cmd);
            dt = new DataTable();
            adp.Fill(dt);

            grdSpanSale.DataSource = dt;
            grdSpanSale.Columns[2].Width = 50;
            grdSpanSale.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            grdSpanSale.Columns[4].Visible = true;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900