Hi there !
how to select row from datagridview when column have cells is empty
I tried this but nothing worked
Please help on this

 private void selectrow()
        {
            int i;
            for( i=0 ;i<dataGridView1 .Rows .Count -1;i++)
            {
                string row = dataGridView1.Rows[i].Cells["Name"].Value.ToString();
                if (row =="")
                {
                    sqlconn.Open();
                    string sqlquery = "select * from tbl... where id='" + dataGridView1.Rows[i].Cells["id"].Value.ToString() + "'";
                    SqlCommand sqlcmd = new SqlCommand(sqlquery, sqlconn);
                    SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
                    DataTable dt = new DataTable();
                    sqlda.Fill(dt);
                    dataGridView1.DataSource = dt;
                    sqlconn.Close();
                }


            }

        }

Hi Fugio, welcome here at Daniweb.
First: line 4: why you substract 1 from Count?
In the loop when i gets equal to Count it will leave the loop anyway, because your test is i < ...Count.

I can delete but I want to filter the cell by selecting it from the table;

void filter()
       {
            try
            {
               if (dataGridView1.Rows.Count > 0 )
               {
                   for (int i =0 ; i<=dataGridView1.Rows.Count ;i++)
                   {
                       for (int j=0; j<=dataGridView1 .Columns .Count ;j++)
                       {
                           string row = dataGridView1.Rows[i].Cells["UserName"].Value.ToString().Trim();
                           if (row =="")
                           {
                               dataGridView1.Rows.RemoveAt(i);
                               i--;
                               //sqlconn.Open();
                               //string sqlquery = "select * from tbl... where id='" + dataGridView1.Rows[i].Cells["UserID"].Value.ToString() + "'";
                               //SqlCommand sqlcmd = new SqlCommand(sqlquery, sqlconn);
                               //SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
                               //DataTable dt = new DataTable();
                               //sqlda.Fill(dt);
                               //dataGridView1.DataSource = dt;
                               //sqlconn.Close();
                           }
                       }
                   }
               }

           }
           catch(Exception e)
           {
               MessageBox.Show(e.ToString());
           }

       }

Image to
Click Here

Edited 3 Years Ago by fugio

Off the top of my head this is the only way to loop through reach row of a DGV (data grid view), check if the rows have any empty cells and then delete that row.

//Only loop through if are rows to loop through
if (dataGridView.Rows.Count > 0)
{
    for (int row = 0; row < dataGridView.Rows.Count; row++)
    {
        for (int col = 0; col < dataGridView.Columns.Count; col++)
        {
            if (dataGridView.Rows[row].cells[col].Value = null || dataGridView.Rows[row].cells[col].Value.ToString() = string.Empty)
            {
                dataGridView.Rows.Remove(dataGridView.Rows[row]);
                break;
            }
        }
    }
}

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.