?
Solved

How do i loop through my gridview to save changed data?

Posted on 2011-09-05
6
Medium Priority
?
328 Views
Last Modified: 2012-05-12
I've pulled some data, into a gridview control. The gridview contains text box controls that are editable. I'd like to save the data that gets edited when the user clicks the / a save button. How do i lool through the rows in the grid view, in the code behind (C#), to process (capture) the changed data for saving?
<asp:UpdatePanel runat="server" ID="upStudentLoginGrid" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="gdvTimeRecs" runat="server" AutoGenerateColumns="false" CssClass="SignedInGrid"
            GridLines="None" AllowSorting="true" AllowPaging="true" PageSize="20" PageIndex="0"
            PagerStyle-HorizontalAlign="Center" OnRowDataBound="gdvTimeRecs_OnRowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Student Name" ItemStyle-CssClass="StudentName">
                    <ItemTemplate>
                        <asp:Label ID="lblStudentName" runat="server" CssClass="StudentNameLabel" Text='<%# Eval("StudentName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Student ID">
                    <ItemTemplate>
                        <asp:Label ID="lblAthleteUid" runat="server" Text='<%# Eval("StudentId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Time In">
                    <ItemTemplate>
                        <%--<asp:Label ID="lblTimeIn" runat="server" Text='<%# Eval("TimeIn") %>'></asp:Label>--%>
                        <asp:TextBox ID="tbxTimeIn" runat="server" Text='<%# Eval("TimeIn") %>' Width="150px"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Time Out">
                    <ItemTemplate>
                        <%--<asp:Label ID="lblTimeOut" runat="server" Text='<%# Eval("TimeOut") %>'></asp:Label>--%>
                        <asp:TextBox ID="tbxTimeOut" runat="server" Text='<%# Eval("TimeOut") %>' Width="150px"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

Open in new window

0
Comment
Question by:Michael Sterling
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:Michael Sterling
ID: 36486236
i should add that the button the user will click to save the changed data, is on a page that contains the user control that contains the grid...
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 36490599
@disrupt: based on your response, (the link) and the research / testing i've done (don't worry you'll get full credit ;-) ) i'm guessing that in order to "grab" the values that populate the grid, the values need to be in TextBox controls? in my code snippet below, ran against the same grid defined above,...i can get the text from the TextBox controls, but the not the <asp:Lable> control, (which on the code side I had to cast to a LiteralControl). Is there a way to grab the value of the label in the grid?
//i get the value that is in the TextBox, (test), but not the value //from the lable (test1)...

        string test = "";
        string test1;
        foreach (GridViewRow row in gdvTimeRecs.Rows)
        {
            TextBox txbx = (TextBox)row.FindControl("tbxTimeIn");
            
            if ( txbx != null)
                test = txbx.Text;

            test1 = ((LiteralControl)(row.Cells[0].Controls[0])).Text;

        }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Accepted Solution

by:
disrupt earned 2000 total points
ID: 36490759
have you tried:

Label testL = (Label)row.FindControl("lblAthleteUid");

0
 
LVL 16

Expert Comment

by:disrupt
ID: 36490774
If you have trouble using the code above and  picking it up you can use a recurisve find :)

http://sharpertutorials.com/recursive-findcontrol/

Note: you wont need to iterate through the gridview with the recursive find.
0
 
LVL 1

Author Closing Comment

by:Michael Sterling
ID: 36490839
that did it. seemed like i'd tried that before. but maybe i'd only tried something similar or close to that, and well,...almost only counts in horse shoes and hand grenades,... so thanks...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question