locked
GridView Edit, Update and Delete RRS feed

  • Question

  • User-1717698715 posted

    Folks i am new to ASP.NET. I have used a GridView control to populate data from Oracle database using NHibernate. It is working fine. Now i want to add edit delete and update functionality into the grid. I have added the Edit and Delete columns and i have also written event handlers for RowEditing, RowUpdating. In the RowUpdating event, i try to get the NewValues and OldValues but i am not able to get them as NewValues.Count and OldValues.Count = 0. Can any one explain why the Count = 0? and How can i get new and old values?

     

    Here is my GridView

    <asp:GridView ID="ptclGrid" runat="server" AutoGenerateColumns="False"
                            CssClass="gridServices"
                            onrowdeleting="ptclGrid_RowDeleting" onrowediting="ptclGrid_RowEditing"
                            onrowupdated="ptclGrid_RowUpdated" onrowupdating="ptclGrid_RowUpdating"
                             DataKeyNames="ID">

    And here is my event handler code

    protected void ptclGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
          {
            //IOrderedDictionary dictionary = e.NewValues;
            //ICollection keys = dictionary.Keys;
            //ICollection values = dictionary.Values;

              int keyCount = e.Keys.Count;
              int NewValueCount = e.NewValues.Count;
              int OldValueCount = e.OldValues.Count;
              int rowIndex = e.RowIndex;

            foreach(DictionaryEntry entry in e.Keys)
            {
               
               
            }
           
        }

    Pl note that keyCount, NewValueCount, OldValueCount = 0 despite the fact that i am updating the values....

     

    Can any one help

    Thursday, March 11, 2010 7:54 AM

Answers

  • User626880745 posted

    <asp:GridView ID="ptclGrid" runat="server" AutoGenerateColumns="False"
                            CssClass="gridServices"
                            onrowdeleting="ptclGrid_RowDeleting" onrowediting="ptclGrid_RowEditing"
                            onrowupdated="ptclGrid_RowUpdated" onrowupdating="ptclGrid_RowUpdating"
                             DataKeyNames="ID">

    Can any one explain why the Count = 0?

    Since you have not associated a datasource control with your gridview these collections would not be populated. you will need to extract those values manually.

    How can i get new and old values?

    You could use the ExtractValuesFromCell method on a BoundField (see an example here: http://forums.asp.net/p/1034786/1427352.aspx)


    Now you could get the values directly too:

    - for BoundFields (they'll be textboxes for each in edit mode):

        string someValue = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[n].Controls[0])).Text; //n is the column index starting at 0


    - for TemplateFields (you'll need to use FindControl):
     
        string someValue = String.Empty;
        TextBox tbox = GridView1.Rows[e.RowIndex].FindControl("yourTextBoxIDHere") as TextBox;
        if (tbox != null)
       {
            someValue = tbox.Text;
       }


    And finally, remember to bind the datasource under a !IPostBack check else you'll get the initial old values instead of the updated ones

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 11, 2010 11:59 AM
  • User3866881 posted

    Yes, since you haven't assigned the Datasource to the GridView (Datasource like SqlDataSource, ObjectDataSource...)

    You cannot get the NewValues and OleValues. You will have to use e.Row.FindControl("Id of TextBox") or e.Row.Cells[index-0-based].Controls[0] as TextBox (for AutoColumns= true). to get the values one by one.

    and when you assign the DataKeyNames for the GridView. You can use GridView.DataKeys[e.RowIndex].Value to get the primary key.

    Thus you can use SqlCommand with these parameters to update the specific row.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 13, 2010 9:19 PM

All replies

  • User16212438 posted
    The key to this one may be in what you did in the rest of the GridView. Please post the full declaration. (<aspGridView...>...</asp:GridView>
    Thursday, March 11, 2010 10:51 AM
  • User278749613 posted

    please have a look at the Msdn documentation ,that will help you

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx

    Thursday, March 11, 2010 11:03 AM
  • User626880745 posted

    <asp:GridView ID="ptclGrid" runat="server" AutoGenerateColumns="False"
                            CssClass="gridServices"
                            onrowdeleting="ptclGrid_RowDeleting" onrowediting="ptclGrid_RowEditing"
                            onrowupdated="ptclGrid_RowUpdated" onrowupdating="ptclGrid_RowUpdating"
                             DataKeyNames="ID">

    Can any one explain why the Count = 0?

    Since you have not associated a datasource control with your gridview these collections would not be populated. you will need to extract those values manually.

    How can i get new and old values?

    You could use the ExtractValuesFromCell method on a BoundField (see an example here: http://forums.asp.net/p/1034786/1427352.aspx)


    Now you could get the values directly too:

    - for BoundFields (they'll be textboxes for each in edit mode):

        string someValue = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[n].Controls[0])).Text; //n is the column index starting at 0


    - for TemplateFields (you'll need to use FindControl):
     
        string someValue = String.Empty;
        TextBox tbox = GridView1.Rows[e.RowIndex].FindControl("yourTextBoxIDHere") as TextBox;
        if (tbox != null)
       {
            someValue = tbox.Text;
       }


    And finally, remember to bind the datasource under a !IPostBack check else you'll get the initial old values instead of the updated ones

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 11, 2010 11:59 AM
  • User3866881 posted

    Yes, since you haven't assigned the Datasource to the GridView (Datasource like SqlDataSource, ObjectDataSource...)

    You cannot get the NewValues and OleValues. You will have to use e.Row.FindControl("Id of TextBox") or e.Row.Cells[index-0-based].Controls[0] as TextBox (for AutoColumns= true). to get the values one by one.

    and when you assign the DataKeyNames for the GridView. You can use GridView.DataKeys[e.RowIndex].Value to get the primary key.

    Thus you can use SqlCommand with these parameters to update the specific row.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 13, 2010 9:19 PM