How to eleminate row from datagridview on specified condition

kau14 0 Tallied Votes 223 Views Share

I have filled the datagridview on the buttonclick

Now, The reuirement is, If the excel having null, or 0 or stringvalue datagridview should eleminate that row.

Please suggest.

Thanks & Regards
Kaustubh

private void btn_choose_excel_Click_1(object sender, EventArgs e)
        {
            string pathconn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + txt_path.Text + ";extended properties=\"Excel 8.0; HDR=yes;\";";
            OleDbConnection cn = new OleDbConnection(pathconn);
            OleDbDataAdapter adp = new OleDbDataAdapter("select * from[" + txt_sheetname.Text + "$]", cn);
            DataTable dt = new DataTable();
            adp.Fill(dt);

            dataGridView1.DataSource = dt;

            List<DataGridViewRow> rowsToRemove = new List<DataGridViewRow>();

            //foreach (DataGridViewRow row in dataGridView1.Rows)
            //{
            //    if (!row.IsNewRow)
            //    {
            //        if (int.Parse(dataGridView1[1, row.Index].Value.ToString()) == 0)
            //        {
            //            rowsToRemove.Add(row);
            //        }


            //    }
            //}
            //foreach (DataGridViewRow row in rowsToRemove)
            //    dataGridView1.Rows.Remove(row);
            this.dataGridView1.Sort(this.dataGridView1.Columns["Name"], ListSortDirection.Ascending);
        }
SteveDotNet 0 Light Poster

You do NOT remove rows from the DataGrid. Remove them from the DataSource.

kau14 0 Newbie Poster

Hi Steve,

Thanks for the reply.
I wont wish to eliminate through the excel,
As datagridview's only purpose to display the valid data.

SteveDotNet 0 Light Poster

Consider:
1. adding a where clause to the query
2. using a DataView as the DataGrid's DataSource

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.