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

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 2
Reputation: amitj is an unknown quantity at this point 
Solved Threads: 0
amitj amitj is offline Offline
Newbie Poster

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

 
0
  #1
Jul 19th, 2008
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

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

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

 
0
  #2
Nov 13th, 2008
you can add new row using dataview

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

bruce.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

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

 
0
  #3
Nov 13th, 2008
Did you enable adding records in your datagridview, and if you're manually adding them have you tried adding them to the datasource?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 172
Reputation: Jugortha is an unknown quantity at this point 
Solved Threads: 16
Jugortha Jugortha is offline Offline
Junior Poster

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

 
0
  #4
Nov 15th, 2008
It is impossible to add a datagridviewrow to a data bound data grid, if you do you will recieve an exception at run time
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C# Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC