Updating DataSet object with a GridView

Last post 09-19-2008 10:39 AM by srkvellanki. 12 replies.

Sort Posts:

  • Updating DataSet object with a GridView

    11-24-2004, 11:27 AM
    • Member
      15 point Member
    • Brainstormer
    • Member since 11-24-2004, 10:37 AM
    • Posts 3
    I'm trying to update a DataSet object with a GridView. However, it seems impossible.

    We have a 3-tier architecture with strict boundaries. Business objects cannot leave the business layer.
    This means that communication between UI components and Business components is done by means of a DataSet.
    This also means we cannot access the database from our presentation layer.

    As a consequence, we cannot use the Data Source Object Model. I tried hard to use one of the Data Source objects to update a DataSet, but was not able to do this.

    So we bind the DataSet object directly to the Data property of the GridView object. This works fine, as long as we just "view" the data.
    When we do an update and catch the event:
    void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    we thought we could get the values from the NewValues collection of the GridViewUpdateEventArgs e.
    However, they are empty!!! (When you use a data source, the collection is filled with the new data).
    Is this a bug???

    So all I want to do is this simple thing:

    ----> We get a DataSet from the business layer, we show it in the presentation layer, do some updates and give it back to the business layer.

    Does anyone know how I can achieve this in asp.net 2.0??
  • Re: Updating DataSet object with a GridView

    01-31-2005, 6:51 AM
    • Member
      155 point Member
    • samychu
    • Member since 01-31-2005, 5:59 AM
    • France-Switzerland
    • Posts 31
    Hi,

    I have now the same problem. I have a dataset for my gridview datasource. And I want to update my dataset whenever the user wants to change the values shown in the gridview. Then update in one hit the database with the dataset (with batch updates).
    I've try to do that, but I haven't find something which could work fine.
    Have you solved your problem ?

    Thanks for your response.
    (sorry for my bad english).
  • Re: Updating DataSet object with a GridView

    01-31-2005, 7:29 AM
    • Member
      15 point Member
    • Brainstormer
    • Member since 11-24-2004, 10:37 AM
    • Posts 3
    No, still have no solution.

    Best,

    Bart
  • Re: Updating DataSet object with a GridView

    08-06-2005, 1:12 PM
    • Participant
      780 point Participant
    • BurntSky
    • Member since 05-04-2004, 11:20 AM
    • Bellevue, WA
    • Posts 156
    Sorry to revive an old topic, but I'm experiencing this exact same problem.  Has anyone discovered a solution?
  • Re: Updating DataSet object with a GridView

    08-07-2005, 9:09 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    Hello.

    Well, you'll just have to go through the controls collection of each cell to get a reference to the controls placed inside it.

    for instance, if you've got a textbox on the 1st column, then you'll have to do something like this to get its text;

    ((TextBiox)grid.Rows[e.RowIndex].Cells[0].Controls[0]).Text

    where e is the eventargs parameter passed to the updating event.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Updating DataSet object with a GridView

    08-07-2005, 12:16 PM
    • Participant
      780 point Participant
    • BurntSky
    • Member since 05-04-2004, 11:20 AM
    • Bellevue, WA
    • Posts 156
     Luis Abreu wrote:

    Hello.

    Well, you'll just have to go through the controls collection of each cell to get a reference to the controls placed inside it.

    for instance, if you've got a textbox on the 1st column, then you'll have to do something like this to get its text;

    ((TextBiox)grid.Rows[e.RowIndex].Cells[0].Controls[0]).Text

    where e is the eventargs parameter passed to the updating event.



    Well, that's an obvious solution, but it doesn't take advantage of the new functionality of the GridView.  What I'm trying to understand is why the NewValues/OldValues/Keys properties of the EventArgs parameter are populated when using one of the built in data sources, but not when manually binding a DataSet to the GridView.
  • Re: Updating DataSet object with a GridView

    08-07-2005, 12:59 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    Hello.

    well, it's just one of the things that stop working when you don't use the data source controls. for instance, sorting and paging don't work without adding code. when you don't use the data source controls those collections aren't filled...that's thw way it is...unfortunatelly, that is...

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Updating DataSet object with a GridView

    08-07-2005, 1:51 PM
    • Participant
      780 point Participant
    • BurntSky
    • Member since 05-04-2004, 11:20 AM
    • Bellevue, WA
    • Posts 156

    Very unfortunate indeed. 

    I suppose it would be possible to extend the GridView control and write methods that fill the collections manually... But I don't need it THAT badly.  Maybe it will be something they add before the release date... although I sincerely doubt it.

    Thanks for your help anyway.

  • Re: Updating DataSet object with a GridView

    10-11-2005, 5:29 PM
    • Member
      20 point Member
    • lukerk
    • Member since 10-11-2005, 7:01 PM
    • Olympia
    • Posts 4
    I, too, have run into this problem. Have you had any luck?
    lucas
  • Re: Updating DataSet object with a GridView

    10-11-2005, 9:18 PM
    • Participant
      780 point Participant
    • BurntSky
    • Member since 05-04-2004, 11:20 AM
    • Bellevue, WA
    • Posts 156
    Unfortunately this isn't something that has been updated in RC1 and probably won't see the actual release version either.  However, a recent project I worked on required a few other extensions of the GridView control (multi-row editing, expanding rows, etc.) so while I was writing my extension I hacked together some code to extract values from a row; it's not very pretty, but it does the job in most cases.  For BoundFields I just grabbed the DataField property and used that as the key in the returned dictionary, but for TemplateFields I didn't have that option so I just used the SortExpression property (which means when implementing the grid, you'll have to specify the SortExpression for your TemplateFields even if sorting isn't enabled; if you don't it just creates a key called "KeyX", where 'X' is the column number).  I apologize if the forum screws up the formatting; bear with me.

    /// <summary>

    /// Extracts all the values from the row contained at the specified index.

    /// </summary>

    /// <param name="rowIndex">The index of the row from which to extract the values.</param>

    /// <returns>IOrderedDictionary containing DataField-Value pairs.</returns>

    public virtual IOrderedDictionary ExtractRowValues(GridViewRow row)

    {

        OrderedDictionary fieldValues = new OrderedDictionary();

     

          for(int i=0; i < this.Columns.Count; i++)

          {

                object value = "";

                string field = "";

                // Extract value from TemplateField

                if(this.ColumnsIdea [I] is TemplateField && row.CellsIdea [I].Controls.Count > 0)

                {

                      // TODO figure out how to grab DataField from two-way binding

                      field = ((TemplateField)this.ColumnsIdea [I]).SortExpression;

                      // Search for a control containing a value

                      foreach(Control c in row.CellsIdea [I].Controls)

                      {

                            if(c is TextBox) { value = ((TextBox)c).Text; break; }

                            else if(c is DropDownList) { value = ((DropDownList)c).SelectedValue; break; }

                            else if(c is CheckBox) { value = ((CheckBox)c).Checked; break; }

                      }

                }

                // Extract value from BoundField

                else if(this.ColumnsIdea [I] is BoundField)

                {

                      field = ((BoundField)this.ColumnsIdea [I]).DataField;

                      if(row.CellsIdea [I].Controls.Count > 0)

                            value = ((TextBox)row.CellsIdea [I].Controls[0]).Text;

                      else

                            value = row.CellsIdea [I].Text;

                }

                // Add the extracted values to the dictionary

                // insert blanks too, so the column count is maintained.

                if(field.Length > 0 && !fieldValues.Contains(field))

                      fieldValues.Add(field, value);

                else

                      fieldValues.Add("Key" + fieldValues.Count, value);

          }

          return fieldValues;

    }

  • Re: Updating DataSet object with a GridView

    03-23-2006, 10:32 AM
    • Member
      10 point Member
    • kita
    • Member since 02-28-2006, 1:49 PM
    • Posts 2

    I got the cause it does not function !!!!

     

    I wrote this code with one failure in it:

     

    If Not Page.IsPostBack Then


                Dim strsql As String = "Select * from Auftrag"
                Dim conn As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings.Item("CoexDBConnectionString").ToString)
                Dim da As New SqlDataAdapter(strsql, conn)

                Dim ds As New DataSet
                da.Fill(ds, "Auftrag")
                da.MissingSchemaAction = MissingSchemaAction.AddWithKey

                Session("ds") = ds
                gv.DataSource = ds
                gv.DataBind()
            Else
                gv.DataSource = CType(Session("ds"), DataSet)
                gv.DataBind()


            End If

     

    When I tried to get the new values out of grid-Textboxes , I always got the old values.

    Because I have overwritten the changes as u can see above.

     

    U have to do it like this:

     

    -delete the else part in the code above

    -get the new values (

    Dim tb2 As TextBox = gv.Rows(e.RowIndex).Cells(0).FindControl("textbox2")

    Response.Write(tb1.Text & tb2.Text)

    )

    -change the dataset

    -save it to viewstate or session

    -bind the grid with the updated dataset

    thats it.

     

     

     


  • Re: Updating DataSet object with a GridView

    08-10-2006, 11:20 AM
    • Member
      62 point Member
    • amelincc
    • Member since 12-17-2002, 9:00 AM
    • New York City
    • Posts 13

    You can use the ObjectDataSource.

    Declare a simple DataHelper class with a method that will return the DataSet you get back from data layer, say DataHelper.GetTheDataSet(string arg)

    and then declare the object data source in you markup like:

    <

    asp:objectdatasource id="MyODS" runat="server" oldvaluesparameterformatstring="original_{0}"

    selectmethod="GetTheDataSet" typename="DataHelper" onselecting="MyODS_Selecting">

    <selectparameters>

    <asp:parameter defaultvalue="-1" name="arg" type="String" />

    </selectparameters>

    </asp:objectdatasource>

     

    Typically you will need to pass some kind of parameter from the UI to the method that will get you the data.  Handle the Selecting event of the object data source and assign the value(s) in the event args that will be passed into the GetTheDataSet() method, which should hit your data layer and get the data back to the grid. 

    It requires a little extra effort, but it gets you paging, sorting and update capabilities.  For updating, I handle the updating event of the grid, do my own thing (hit the db with the new values) and then cancel the updating event in the grid and reset the edit index to -1 so that the editing row goes back to view mode.

  • dynamically add a new row to a existing GridView

    09-19-2008, 10:39 AM
    • Member
      17 point Member
    • srkvellanki
    • Member since 02-25-2005, 12:41 AM
    • Posts 63

     Hi Luis Abreu,

       Using the GridView i display AvailableLocation(textbox),Products(dropdown),Status(Check box).On click of "Add" button,new row should be displayed at the top of the existing GridView,User keyins the textbox value,selects a value from the dropdown,and checks the Check box and Saves the row.

              How do i dynamically add a new row to a existing GridView.

    Regards
    Sri

Page 1 of 1 (13 items)