Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am using a datagridview in a C# window application.
I want to count how many rows in datagridview in runtim.


I attach data in datagridview of database.

Please Help me.

Thanks in Advance
Posted
Updated 12-May-10 19:31pm
v2
Comments
Ankur\m/ 13-May-10 1:06am    
If an answer, answers your question, you should select "Accept Answer" so that the question goes away from the 'Unanswered List'. You could also vote the answer depending on how well it answered your question.

To get the Row Count all you have to do is check the property RowCount of the DataGridView
 
Share this answer
 
 
Share this answer
 
Hello Mr.Kahlid her's your solution
The below code is written in combobox selected index change u can write it in button click, it will count the total number of rows present in grid view and print it in a messagebox

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x=0;
            SqlConnection connect = new SqlConnection();
            connect.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
            connect.Open();
            string mycommand = "select * from Inquiry_Table where time_of_inquiry='" + comboBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(mycommand, connect);
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataTable dt;
            adp.Fill(ds);
            dt = ds.Tables[0];
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                dataGridView1.Visible = true;
                dataGridView1.DataSource = dt; //binding datagrid with data
                x = x + 1; //Increment the value of x         
            }
//printing total count
            MessageBox.Show("Total Inquiry found " + x.ToString(),"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            connect.Close();

        }


Do rate my answer once u find it useful

Thanks & Regards
Radix :)
 
Share this answer
 
If you are binding your gridview to a collection, you can actually get the count of the collection to get a count of your rows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900