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
  1. Home
  2. Web Development
  3. ASP.NET
  4. Bulk updating values from GridView bound to a datatable [moved]

Bulk updating values from GridView bound to a datatable [moved]

Scheduled Pinned Locked Moved ASP.NET
questiondatabasealgorithmshelpdiscussion
2 Posts 1 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.
  • 6 Offline
    6 Offline
    6 921 364 and growing
    wrote on last edited by
    #1

    I feel it may require threaded discussion so I am moving the question here from Q&A. Greetings everyone! This may be a very simple question but I just can't get it working. Moreover Google doesn't seem to help me much (Or I still have a lot to improve on my searching skills. :) ) Let me explain the scenario: This page is basically accessed by a user who has received some items and has to update this page accordingly. I have a GridView bound to a DataTable. The GridView has a checkbox column, few databound columns and a TemplateColumn with a TextBox as ItemTemplate. The GridView has to be updated with two things - a) By unchecking a checkbox, the user indicates that the particular item has not been received. So a flag indicating unreceived will be marked in the database. b) The textbox in the template field will by default contain quantity sent for that particular item. The user can update it to actual quantity received. After all edits are done, user clicks the save button and details (a) and (b) has to be updated to the database at a time. What I have tried: I save the initial GridView value in a ViewState. When the user clicks 'Save' I thought I will get the DataSource and compare the values and do a bulk update using a DataAdapter. But the DataSource is null (which is correct). I do not seem to be able to use things like DataTable.RowState for bulk update. How do I synchronize the GridView with the bound DataTable? Please help me on how should I go about it. Thanks!

    6 1 Reply Last reply
    0
    • 6 6 921 364 and growing

      I feel it may require threaded discussion so I am moving the question here from Q&A. Greetings everyone! This may be a very simple question but I just can't get it working. Moreover Google doesn't seem to help me much (Or I still have a lot to improve on my searching skills. :) ) Let me explain the scenario: This page is basically accessed by a user who has received some items and has to update this page accordingly. I have a GridView bound to a DataTable. The GridView has a checkbox column, few databound columns and a TemplateColumn with a TextBox as ItemTemplate. The GridView has to be updated with two things - a) By unchecking a checkbox, the user indicates that the particular item has not been received. So a flag indicating unreceived will be marked in the database. b) The textbox in the template field will by default contain quantity sent for that particular item. The user can update it to actual quantity received. After all edits are done, user clicks the save button and details (a) and (b) has to be updated to the database at a time. What I have tried: I save the initial GridView value in a ViewState. When the user clicks 'Save' I thought I will get the DataSource and compare the values and do a bulk update using a DataAdapter. But the DataSource is null (which is correct). I do not seem to be able to use things like DataTable.RowState for bulk update. How do I synchronize the GridView with the bound DataTable? Please help me on how should I go about it. Thanks!

      6 Offline
      6 Offline
      6 921 364 and growing
      wrote on last edited by
      #2

      I finally got that working. It was simple as I expected just that I didn't put much effort. This is what I did: User does the required edits (unchecking the checkbox for not received and editing the exact received quantity) an select 'Save'. On Save button click, I get the old data which was saved in ViewState into a datatable. Now I compare the GridView values to the values in DataTale (old values). If it has changed, I edit the value in the datatable. This changes the RowState of that row in the datatable. The code seems like this:

      DataTable dtChallanDtl = new DataTable();
      dtChallanDtl = (DataTable)ViewState["ChallanDtl"];
      int i = 0;
      int ChallanRcvd = 1;
      foreach (GridViewRow r in gvChallanDtl.Rows)
      {
      CheckBox chk = (CheckBox)r.FindControl("CheckSelect");
      TextBox txt = (TextBox)r.FindControl("txtRecvQty");
      if (chk.Checked == true)
      {
      dtChallanDtl.Rows[i]["Received"] = 1;
      }
      else
      {
      ChallanRcvd = 2;
      }
      if (dtChallanDtl.Rows[i]["RecvQty"].ToString() != txt.Text)
      {
      dtChallanDtl.Rows[i]["RecvQty"] = txt.Text;
      dtChallanDtl.Rows[i]["Received"] = 2;
      ChallanRcvd = 2;
      }
      i++;
      }

      Here, the variable 'ChallanRcvd' represents the database column which stores the received status of the challan. 0-not received, 1-received, 2-partailly received. Now I do a bulk update using SqlDataAdapter's Update method. The important things here are: SqlDataAdapterObj.UpdateBatchSize = 4; //how many records need to be updated in each batch. SqlCommandObj.UpdatedRowSource = UpdateRowSource.None; //how command results are applied to the datarow. After all parameters are supplied, call SqlDataAdapterObj.Update(dtChallanDtl); I posted this question here because I couldn't find any example on bulk edit using GridView bound to a datatable. I hope it helps someone. Regards Test

      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