0

I am trying to filter a Datagirdview with combobox value

the code is here,

OleDbDataAdapter dt = new OleDbDataAdapter();
            DataTable tt = new DataTable();
            BindingSource bs = new BindingSource();
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Rock.accdb";
            con.Open();
            string query = "SELECT * FROM bankdet WHERE [date] Between @a AND @b";
            OleDbCommand cmd = new OleDbCommand(query, con);
            cmd.Parameters.AddWithValue("@a", dateTimePicker1.Text);
            cmd.Parameters.AddWithValue("@b", dateTimePicker2.Text);
            dt.SelectCommand = cmd;
            dt.Fill(tt);
            bs.DataSource = tt;
            dataGridView1.DataSource = bs;
            dt.Update(tt);
            con.Close();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string s = row.Cells[1].Value.ToString();
                if (!s.StartsWith(comboBox2.Text, true, null))
                {
                    CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                    currencyManager1.SuspendBinding();
                    row.Visible = false;
                    currencyManager1.ResumeBinding();
                }
                else
                    row.Visible = true;
            }

the first half fill the datagirdview with values, the sencond one must filter it... instead i am getting error at

string s = row.Cells[1].Value.ToString();

any idea why?

1 Answers1

0
row.Cells[1]

or

row.Cells[1].Value

returns null. Than you are trying to invoke Value or ToString() on the null.

So you have to put a break point and see what is happening there.

SaneDeveloper
  • 2,401
  • 3
  • 28
  • 51