6 Replies - 1624 Views - Last Post: 25 July 2013 - 05:41 PM Rate Topic: -----

#1 fugio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 21-July 13

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

Posted 21 July 2013 - 03:57 AM

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

 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();
                }
               

            }
           
        }


Is This A Good Question/Topic? 0
  • +

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

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,515
  • Joined: 08-April 09

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

Posted 21 July 2013 - 04:17 AM

Exactly what is not working, have you tried debugging, placing breakpoint on line 6 to see what is returned?
If the grid is bound to a DataTable then "empty" cells will contain DBNull.Value. If the grid is bound to something else or not bound at all then "empty" cells will contain Nothing.

This post has been edited by Michael26: 21 July 2013 - 04:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 fugio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 21-July 13

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

Posted 25 July 2013 - 01:42 AM

I can delete but I want to filter the cell by selecting it from the table;
manipulating the same offer can help you understand

Posted Image

 void filter()
        {
             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());
            }
           
        }

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,943
  • Joined: 05-May 12

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

Posted 25 July 2013 - 05:09 AM

Why do you have a double for loop? One should be sufficient.

You are already selecting the cell by referencing the row number and column name. Did you just copy the paste the code from somewhere else and not understood what it does?
Was This Post Helpful? 0
  • +
  • -

#5 fugio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 21-July 13

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

Posted 25 July 2013 - 09:39 AM

I just put out different forms. but I can not exactly filter cell empty.
Remove delete option. I filter select table but only display one row.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,943
  • Joined: 05-May 12

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

Posted 25 July 2013 - 05:12 PM

Are you saying that line 12 on your code never becomes true? I'm asking because lines 11-12 is doing exactly what you are asking for: being able to select a cell and determine if the cell is empty.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3329
  • View blog
  • Posts: 11,259
  • Joined: 12-December 12

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

Posted 25 July 2013 - 05:41 PM

You can filter a DataGridView using its DataTable's DefaultView.RowFilter property. There is an example on the linked page, using:

DataTable gridTable = (DataTable) dataGrid1.DataSource;
gridTable.DefaultView.RowFilter = "CompanyName < 'I'";

I assume your criteria would be:

"UserName Is Null"

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1