user3808299
user3808299

Reputation: 3

number of rows in datagridview C#

I have a table in my datagridview and i have dynamically created textboxes in the same form. I want to know the number of my columns and rows but its value is always showing 0

        int j = dmrc.dataGridView2.Rows.Count;
        int k = dmrc.dataGridView2.ColumnCount; 

        for (int i = 0; i<=j+2; i++)
        {
            TextBox txtRun = new TextBox();
            txtRun.Name = "tb"+i;
            txtRun.Location = new System.Drawing.Point(20 , 18 +(25*i));
            txtRun.Size = new System.Drawing.Size(100, 25);
            this.Controls.Add(txtRun);
        }
        for (int i = 0; i <= j+2; i++)
        {
            TextBox txtRun = new TextBox();
            txtRun.Name = "tb" +i;

            txtRun.Location = new System.Drawing.Point(150, 18 + (25* i));
            txtRun.Size = new System.Drawing.Size(100, 25);
            txtRun.Text = Convert.ToString(j); 
            this.Controls.Add(txtRun);
        }

BEFORE THIS I HAD MADE A OleDB CONNECTION TO IMPORT EXCEL FILE

try {

            DataGridView dataGridview2 = new DataGridView();
            dmrc dmrc = new dmrc();

            string pathconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path.Text + "; Extended Properties=\"Excel 8.0;HDR=Yes;\";";
            OleDbConnection conn = new OleDbConnection(pathconn);

            OleDbDataAdapter mydata = new OleDbDataAdapter("Select * from [" + sheet.Text + "$]", conn);
            DataTable dt = new DataTable();

            mydata.Fill(dt);





            dmrc.dataGridView2.DataSource = dt;
            dmrc.Show();



            int j = dmrc.dataGridView2.Rows.Count;
                for (int i = 0; i < j ; i++)
                {


                    string StrQuery = "INSERT INTO " + textBox1.Text + " VALUES ('" + dmrc.dataGridView2.Rows[i].Cells[0].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[1].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[2].Value + "' , '" + dmrc.dataGridView2.Rows[i].Cells[3].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[4].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[5].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[6].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[7].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[8].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[9].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[10].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[11].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[12].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[13].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[14].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[15].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[16].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[17].Value + "','" + dmrc.dataGridView2.Rows[i].Cells[18].Value + "');";

                    try
                    {
                        string myConne = "datasource=localhost;database=dmrc;port=3306;username=root;password=root";
                        MySqlConnection conne = new MySqlConnection(myConne);
                        conne.Open();

                        using (MySqlCommand comm = new MySqlCommand(StrQuery, conne))
                        {
                            comm.ExecuteNonQuery();
                        }
                        conne.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

IN THIS BUTTON MY ROWS.COUNT IS WORKING PERFECT BUT NOT IN THE PREVIOUS ONE

Upvotes: 0

Views: 3463

Answers (1)

Mark C.
Mark C.

Reputation: 6450

One thing to remember is that an "empty" datagridview has 1 record only if the AllowUsersToAddRow property is set to true. Otherwise, the row count will be 0.

Source

I think you're also confused on the syntax to retrieve the Count ..

DataGridView1Name.Rows.Count

Upvotes: 4

Related Questions