943,704 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 7726
  • C# RSS
Jul 19th, 2008
0

Add a new Row in a datagridview which is binded to a datatable

Expand Post »
I fetch the data datafrom database in to a datatable and bind it to a datagridview.Now I want to edit the data shown in the datagridviewand also want to add new rows in dgv.The editing part is going ok but I m not able to add new rows in dgv.Also I want S.No. column to be autogenerated on new row...the coding is below

C# Syntax (Toggle Plain Text)
  1. if (con.State == ConnectionState.Closed)
  2. {
  3. con.Open();
  4. }
  5. DataColumn Col;
  6.  
  7. Col = new DataColumn();
  8. Col.DataType = System.Type.GetType("System.String");
  9. Col.ColumnName = "S.No.";
  10. dt_medilist1.Columns.Add(Col);
  11. SqlDataAdapter adp1 = new SqlDataAdapter("Select Drugs,Schedule,Instructions,(DateTo)Till,Route,(Qty)Quantity,Comments from tblTreatment where Patient=111", con);
  12. adp1.Fill(dt_medilist1);
  13. int rows = dt_medilist1.Rows.Count;
  14.  
  15. for (int i = 0; i < rows; i++)
  16. {
  17. dt_medilist1.Rows[i]["S.No."] = i + 1;
  18. }
  19. dataGridView1.DataSource = dt_medilist1;
Last edited by Tekmaven; Jul 19th, 2008 at 2:30 pm. Reason: Code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amitj is offline Offline
2 posts
since Jul 2008
Nov 13th, 2008
0

Re: Add a new Row in a datagridview which is binded to a datatable

you can add new row using dataview

http://vb.net-informations.com/datav...ew-new-row.htm

bruce.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bruce2424 is offline Offline
23 posts
since Jun 2008
Nov 13th, 2008
0

Re: Add a new Row in a datagridview which is binded to a datatable

Did you enable adding records in your datagridview, and if you're manually adding them have you tried adding them to the datasource?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Nov 15th, 2008
0

Re: Add a new Row in a datagridview which is binded to a datatable

It is impossible to add a datagridviewrow to a data bound data grid, if you do you will recieve an exception at run time
Reputation Points: 11
Solved Threads: 16
Junior Poster
Jugortha is offline Offline
172 posts
since Oct 2007

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: about streaming video file
Next Thread in C# Forum Timeline: Reflection





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


Follow us on Twitter


© 2011 DaniWeb® LLC