Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2011-09-05
6
323 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:mikesExpertExchange
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:mikesExpertExchange
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 16

Expert Comment

by:disrupt
ID: 36486246
0
 
LVL 1

Author Comment

by:mikesExpertExchange
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 16

Accepted Solution

by:
disrupt earned 500 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:mikesExpertExchange
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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