944,138 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 131675
  • C# RSS
Aug 23rd, 2006
0

How to add rows from dataset to datagridview

Expand 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
C# Syntax (Toggle Plain Text)
  1. dgClient.Columns.Add("client", "Client")
  2. dgClient.Columns("client").Width = 150
  3. dgClient.Columns.Add("comments", "Comments")
  4. dgClient.Columns("comments").Width = 250
  5.  
  6. Dim i As Integer
  7. For i = 0 To dsClient.Tables("TableClient").Rows.Count - 1
  8. dgClient.Rows.Add()
  9. dgClient.Rows(i).Cells("client").Value = dsClient.Tables("TableClient").Rows(i).Item("Client")
  10. dgClient.Rows(i).Cells("comments").Value = dsClient.Tables("TableClient").Rows(i).Item("Comments")
  11. Next

How I can do above using C#

Thanks in advance
Last edited by Nick Evan; Nov 23rd, 2009 at 10:31 am. Reason: Add code-tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Oct 10th, 2006
0

Re: How to add rows from dataset to datagridview

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
edreflak is offline Offline
1 posts
since Jun 2006
Jul 3rd, 2009
0

Re: How to add rows from dataset to datagridview

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 .
c# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imtufail is offline Offline
1 posts
since Dec 2008
Mar 4th, 2011
0
Re: How to add rows from dataset to datagridview
tanks ................................
Last edited by m_gh660; Mar 4th, 2011 at 7:11 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m_gh660 is offline Offline
1 posts
since Mar 2011
Mar 7th, 2011
0
Re: How to add rows from dataset to datagridview
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.
C# Syntax (Toggle Plain Text)
  1. gridView1.DataSource= DS1
  2. gridView1.DataBind()

Hope it will help u.

Regards
Kalyan.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akshintlakalyan is offline Offline
11 posts
since Feb 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: "item not found" exception
Next Thread in C# Forum Timeline: Create new object !?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC