Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. adding AddingRows to dataTable

adding AddingRows to dataTable

Scheduled Pinned Locked Moved C#
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sangramkp
    wrote on last edited by
    #1

    Hi, i am trying to add dataRow to dataTable which in turn poulates a datagridview. But the dtaGridView shows the lattest row added. I want to show all the data Using datatable. someone plz help....

    Sangram (A battle with self) Life is simple, we are the ones makes the living difficult

    P 1 Reply Last reply
    0
    • S sangramkp

      Hi, i am trying to add dataRow to dataTable which in turn poulates a datagridview. But the dtaGridView shows the lattest row added. I want to show all the data Using datatable. someone plz help....

      Sangram (A battle with self) Life is simple, we are the ones makes the living difficult

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      Post your code. This will help others answer your question. Paul

      S 1 Reply Last reply
      0
      • P pmarfleet

        Post your code. This will help others answer your question. Paul

        S Offline
        S Offline
        sangramkp
        wrote on last edited by
        #3

        code< protected void btnSlabAdd_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt= new DataTable(); DataColumn dc1 = new DataColumn("StartAmount", typeof(double)); DataColumn dc2= new DataColumn("EndAmount", typeof(double)); DataColumn dc3= new DataColumn("AccountShare", typeof(double)); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); DataRow dr = dt.NewRow(); if (dt.Rows.Count == 0) { dr["StartAmount"] = 1; dr["EndAmount"] = double.Parse(txtEndAmount.Text); dr["AccountShare"] = double.Parse(txtAccountShare.Text); //dt.Rows.Add(dr); } else { //dr["StartAmount"] = 1 + double.Parse(dt.Rows[dt.Rows.Count - 1].Table.Columns["EndAmount"].Caption); dr["StartAmount"] = 1 + double.Parse(gdvDistributorSlab.Rows[gdvDistributorSlab.Rows.Count - 1].Cells[2].Text); dr["EndAmount"] = double.Parse(txtEndAmount.Text); dr["AccountShare"] = double.Parse(txtAccountShare.Text); //dt.Rows.Add(dr); } dt.Rows.Add(dr); gdvDistributorSlab.DataSource = dt; gdvDistributorSlab.DataBind(); } > As in the above code I was trying to add datarows to a dataTable and viewing it in gridView But Everytime I can see only One row in gridView...... I want to see all the rows added to the datatable.... help please

        Sangram (A battle with self) Life is simple, we are the ones makes the living difficult

        P 1 Reply Last reply
        0
        • S sangramkp

          code< protected void btnSlabAdd_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt= new DataTable(); DataColumn dc1 = new DataColumn("StartAmount", typeof(double)); DataColumn dc2= new DataColumn("EndAmount", typeof(double)); DataColumn dc3= new DataColumn("AccountShare", typeof(double)); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); DataRow dr = dt.NewRow(); if (dt.Rows.Count == 0) { dr["StartAmount"] = 1; dr["EndAmount"] = double.Parse(txtEndAmount.Text); dr["AccountShare"] = double.Parse(txtAccountShare.Text); //dt.Rows.Add(dr); } else { //dr["StartAmount"] = 1 + double.Parse(dt.Rows[dt.Rows.Count - 1].Table.Columns["EndAmount"].Caption); dr["StartAmount"] = 1 + double.Parse(gdvDistributorSlab.Rows[gdvDistributorSlab.Rows.Count - 1].Cells[2].Text); dr["EndAmount"] = double.Parse(txtEndAmount.Text); dr["AccountShare"] = double.Parse(txtAccountShare.Text); //dt.Rows.Add(dr); } dt.Rows.Add(dr); gdvDistributorSlab.DataSource = dt; gdvDistributorSlab.DataBind(); } > As in the above code I was trying to add datarows to a dataTable and viewing it in gridView But Everytime I can see only One row in gridView...... I want to see all the rows added to the datatable.... help please

          Sangram (A battle with self) Life is simple, we are the ones makes the living difficult

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          Each time the event fires, you are creating a new DataTable object, adding a row to it and binding it to your control. This means that you are discarding any data that you had previously bound to the control. If you want to append your new row of data to the data you have already created you should replace

          DataTable dt= new DataTable();

          with...

          DataTable dt = null;

          if (gdvDistributorSlab.DataSource == null)
          {
          dt= new DataTable;
          }
          else
          {
          dt = (DataTable)gdvDistributorSlab.DataSource;
          }

          Paul

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups