DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: ASP.NET 2.0 GridView nightmare!!!

  1. #1
    Join Date
    Oct 2005

    ASP.NET 2.0 GridView nightmare!!!

    I'm after some help before I go insane!!

    I've been trying to crack this problem for quite a few days and can't find a solution so any help much appreciated.

    I am trying to create a multi-row edit GridView so users can edit more than one row at a time without posting back. I want the underlying object(s) to be saved only when the user clicks the save button. I also want the user to be able to add multiple blank rows by clicking another button.

    So my GridView is bound to an ObjectDataSource. My ObjectDataSource contains a collection..... List<Entries> which gets loaded by forcing ObjectDataSource to bind to my object in the code behind page hooking into the _ObjectCreating event, rather than creating it's own instance of my specified TyeName.

    So far so good.

    My first problem is the add multiple rows button. I have a dropdownlist above my grid with numbers 1- 20 and an add button. when the user clicks add I hook into the _add_button_click event. Now, the GridViewRowCollection is protected so you cannot add rows programattically. So I thaught I'd add a new 'Entry' object (one for each number required in the dropdown) to my List<Entries> then rebind the grid and it would automatically add the required amount of rows based on the <Entries> in my List.

    This works fine, but if I already have a load of rows in my grid and the user has added data by editing the textboxes or dropdowns this gets totally lost on postback when they try and add new rows. I'm assuming this is happening due the rebinding of the grid?

    And this brings me on to the second problem when the user clicks the save button I cannot seem to get the data from the grid on postback. The Grid semes to have no rows and the ViewState is empty. It's like there's some event causing my grid to lose it's contents before I try and read it out....... I missing something fundamental here??? Has anyone tried using the GridView in a similar way?? Is there an easy way to add rows tot he grid without rebinding??

    If I implemented the single row edit functionality then all would be straight forward but that's not what I want. I need a more spreadsheet like GridView.

    Thanks in advance ....

  2. #2
    Join Date
    Feb 2007
    I have similar problem. Does anyone have any suggestions?

  3. #3
    Join Date
    Jul 2007

    Anyone got any info on this?

    I have accomplished a similar feat, but i had to jump through lots of hoops to do it. If anyone has any info on this or would like to see my hack of a solution, post here please.

  4. #4
    Join Date
    Oct 2005

    Cool My solution

    Quote Originally Posted by lonestar.mike
    were you able to work out the solution to your multi-row edit, multi-row add gridview from your post back in 2005?
    Hi Mike, yes after much playing worked out a great solution (back in 2005) which is still being hammered in my web portal today!

    I use a GridView with the ObjectDatasource.

    In your aspx page add the relevant control to the <ItemTemplate> section for each GridView column. In my grid I use labels, textbox, date and dropdownlist controls.

    Next you have to hook into the MyGridView_RowDataBound event.

    Inside here check what row you're binding eg.
    if (e.Row.RowType == DataControlRowType.DataRow)
        //do stuff here
    You can access the header, footer or data rows here and do almost aything you like.

    I cast my object from the row e.g.

    MyObject obj = (MyObject)e.Row.DataItem

    Then I access each control from the row and populate it from my object's properties.

    Something like:
    TextBox txtName = (TextBox)e.Row.FindControl("txtName");
    ' now assign a value
    txtName.Text = obj.Name;
    Now you have the server control you can also change any properties you like such as the CSS, enable it or disbale it, show or hide etc.

    If you are using a simple a GridView and don't need to tweak all the controls as I did, you can probably use DataBinding within the Text="" property of the TextBox for example inside the <ItemTemplate> block in the .aspx page.

    Adding multiple rows is also easy. I simply have an 'Add' button and a dropdown list with 1 to 50 sitting in a <div> above my grid. I hook up a postback on the Add button OnClick event. In the code behind my click event reads the number from the dropdown then instanciates x number of new MyObjects, adds them to the collection in my ObjectDataSource then I simply call MyGridView.Databind(). Hey presto it re-renders with all the new rows etc. Simple.

    Hope that all makes sense. Have fun!!!!


    Last edited by Hack; 12-03-2008 at 02:48 PM.

  5. #5
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    Thank you for coming back and sharing your solution.

    Hopefully, it will help others with the same or similiar problem.

  6. #6
    Join Date
    Nov 2008

    help pleaaase

    Dear seasider,
    I am having a similar scenario,where i am using Dataset as datasource + .NET2.0 + VB.NET 2005.
    Unfotunately i am not able to get it to work.
    could u please attach for me your code?
    i really need this to work and i apreciate your help.waiting for ur reply, thanks again

  7. #7
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    He did post his code in Post #4

  8. #8
    Join Date
    Nov 2008

    indeed but...

    i have red that code... but it is odd for me since i am having a more simple case.. i wish if could be somehow more clear.
    i just added a textbox in <ItemTemplate> for each <TemplateField> column
    and i need to first read the data from dataset and fill my GridView and then have an Add button which adds a new row on click ( for each column).
    here is what i've done:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Style="z-index: 106;
                left: 40px; position: absolute; top: 188px" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <asp:TemplateField HeaderText="Milestone">
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:TemplateField HeaderText="Planned date">
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    <asp:TemplateField HeaderText="Actual Date">
                            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    <asp:TemplateField HeaderText="% Complete">
                            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    <asp:TemplateField HeaderText="Progress Date">
                            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                    <asp:TemplateField HeaderText="Comments">
                            <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
    I have included a .GIF file showing what exactly i want. Though i dont have any comboboxes.
    i really appreciate any help since i'm short on time...
    many thanks
    Attached Images Attached Images

Similar Threads

  1. Master Pages in ASP.NET 2.0 ???
    By salsipius in forum ASP.NET
    Replies: 1
    Last Post: 12-31-2008, 12:48 PM
  2. DataGrid [ ASP.NET 2.0 ]
    By Sync in forum .NET
    Replies: 2
    Last Post: 09-17-2005, 01:09 AM
  3. Replies: 4
    Last Post: 09-02-2005, 08:22 PM
  4. The quwstion about ASP.NET 2.0
    By liliarum in forum ASP.NET
    Replies: 3
    Last Post: 06-21-2005, 05:28 AM
  5. Free ASP.NET Web Matrix Design/Editor Tool Released
    By ASPSmith Training in forum dotnet.announcements
    Replies: 0
    Last Post: 06-18-2002, 03:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center