Solved

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

Posted on 2011-09-05
6
320 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now