How to add rows from dataset to datagridview

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

How to add rows from dataset to datagridview

 
0
  #1
Aug 23rd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1
Reputation: edreflak is an unknown quantity at this point 
Solved Threads: 0
edreflak edreflak is offline Offline
Newbie Poster

Re: How to add rows from dataset to datagridview

 
0
  #2
Oct 10th, 2006
Originally Posted by Swapnil Palande View Post
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: imtufail is an unknown quantity at this point 
Solved Threads: 0
imtufail imtufail is offline Offline
Newbie Poster

Re: How to add rows from dataset to datagridview

 
0
  #3
Jul 3rd, 2009
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 .
  1. conString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
  2. con = new SqlConnection(conString);
  3. con.Open();
  4. string quryString = " select * from Employees";
  5. da = new SqlDataAdapter(quryString, con);
  6. ds = new DataSet();
  7. da.Fill(ds, "Emp");
  8. //dataGridView1.DataSource = ds.Tables["Emp"];
  9. dataGridView1.Columns.Add("EmpID", "ID");
  10. dataGridView1.Columns.Add("FirstName", "FirstName");
  11. dataGridView1.Columns.Add("LastName", "LastName");
  12. int row = ds.Tables["Emp"].Rows.Count - 1;
  13. for (int r = 0; r<= row; r++)
  14. {
  15. dataGridView1.Rows.Add();
  16. dataGridView1.Rows[r].Cells[0].Value = ds.Tables["Emp"].Rows[r].ItemArray[0];
  17. dataGridView1.Rows[r].Cells[1].Value = ds.Tables["Emp"].Rows[r].ItemArray[1];
  18. dataGridView1.Rows[r].Cells[2].Value = ds.Tables["Emp"].Rows[r].ItemArray[2];
  19. }
  20.  
  21.  
  22.  
  23. con.Close();
  24. }
Last edited by Ancient Dragon; Jul 3rd, 2009 at 1:59 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC