?
Solved

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

Posted on 2011-09-05
6
Medium Priority
?
326 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

743 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