Skip to content

How to add rows from dataset to datagridview

Swapnil Palande

Asked Aug 22, 2006, 10:11 PM EDT1 answer
Share
LinkedInX
Hi all,

Please solve my Problem:

I have DataGridView and Dataset. I want to add data from dataset to datagridview row by row and not by using DataGridView.DataSource method.

For ex. following is the way to do the same using Vb.net

dgClient.Columns.Add("client", "Client")
dgClient.Columns("client").Width = 150
dgClient.Columns.Add("comments", "Comments")
dgClient.Columns("comments").Width = 250

Dim i As Integer
For i = 0 To dsClient.Tables("TableClient").Rows.Count - 1
dgClient.Rows.Add()
dgClient.Rows(i).Cells("client").Value = dsClient.Tables("TableClient").Rows(i).Item("Client")
dgClient.Rows(i).Cells("comments").Value = dsClient.Tables("TableClient").Rows(i).Item("Comments")
Next

How I can do above using C#

Thanks in advance

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Here is your C# code               

                    dgClient.Columns.Add ("client", "Client");
                    dgClient.Columns["client"].Width = 150;
                    dgClient.Columns.Add("comments", "Comments");
                    dgClient.Columns["comments"].Width = 250;

                    for(int i= 0; i<= dsClient.Tables["TableClient"].Rows.Count - 1; i++)
                    {
                        dataGridView1.Rows.Add();
                        dgClient.Rows[i].Cells["client"].Value = dsClient.Tables["TableClient"].Rows[i]["Client"];
                        dgClient.Rows[i].Cells["comments"].Value = dsClient.Tables["TableClient"].Rows[i]["Comments"];

                    }

    0