how to select row from datagridview when column have cells is empty

fugio

New member
Joined
Jul 21, 2013
Messages
3
Programming Experience
Beginner
Hi there !
how to select row from datagridview when column have cells is empty
I tried couple of this but nothing worked
Please help on this
How To Select Row From Datagridview When Column Have Cells Is Empty.png
C#:
[COLOR=#000000]void filter()[/COLOR]       {
            try
            {
               if (dataGridView1.Rows.Count > 0 )
               {
                   for (int i =0 ; i<=dataGridView1.Rows.Count ;i++)
                   {
                       for (int j=0; j<=dataGridView1 .Rows .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());
           }
           [COLOR=#000000]       }[/COLOR]
 
Are you saying that you want to display only the rows where UserName is empty? Is the grid bound to anything, e.g. a DataTable? If not, is there any reason that it couldn't be? It's just that filtering bound data is much easier.
 

Latest posts

Back
Top Bottom