1.11M Members

How to add rows from dataset to datagridview

 
0
 

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

 
0
 

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

Hi Swap, you will have to do some databinding and invoke the CurrencyManager as well. There is also a BindingManagerBase class that should prove to be usefull as well.
See www.akadia.com/services/dotnet_databinding.html

See ya
edreflak

 
0
 

here is the equivalent code that u have given in VB, but plz plz this is my first time that i have writen code for the datagridview in C#. i m begginer too, to seach and learn abt datagridview when i found your problem so i tried on it . if it is correct so plz tell me. otherwise sorry for disturbing u .

conString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
            con = new SqlConnection(conString);
            con.Open();
            string quryString = " select * from Employees";
            da = new SqlDataAdapter(quryString, con);
            ds = new DataSet();
            da.Fill(ds, "Emp");
            //dataGridView1.DataSource = ds.Tables["Emp"];
            dataGridView1.Columns.Add("EmpID", "ID");
            dataGridView1.Columns.Add("FirstName", "FirstName");
            dataGridView1.Columns.Add("LastName", "LastName");
            int row = ds.Tables["Emp"].Rows.Count - 1;
            for (int r = 0; r<= row; r++)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[r].Cells[0].Value = ds.Tables["Emp"].Rows[r].ItemArray[0];
                dataGridView1.Rows[r].Cells[1].Value = ds.Tables["Emp"].Rows[r].ItemArray[1];
                dataGridView1.Rows[r].Cells[2].Value = ds.Tables["Emp"].Rows[r].ItemArray[2];
            }
	        


            con.Close();
        }
 
0
 

tanks ................................

 
0
 

First of all u get the all required rows and columns from the Database to a DataSet using DataAdapter. Then here by using the DataBind Property of the Gridview u can bind to the Gridview, that will automatically add columns and rows. And in future if u want to add Extra columns or rows add them first to the Dataset's Table and again bind to the gridview.

gridView1.DataSource= DS1
gridView1.DataBind()

Hope it will help u.

Regards
Kalyan.

 
0
 

i want to calculate 2 cells in my grid view for eg: quantity 1 cell and rate another cell (quantity * rate = amount) how can i multiply and get the answer in run time in cell amount can u help me out

 
0
 

i want to calculate 2 cells in my grid view for eg: quantity 1 cell and rate another cell (quantity * rate = amount) how can i multiply and get the answer in run time in cell amount can u help me out

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article