?
Solved

How to loop through grid view values at runtime and gather changes

Posted on 2013-01-31
2
Medium Priority
?
221 Views
Last Modified: 2013-02-01
When I click on "Save" I need to loop through the gridview rows one by one and gather the following info:

RepairBidID
RepairBidSID
Status

status

I've not done this in a while.  I need some help.  What does this typically look like?


Here is the markup for the gridview portion:

 <cc1:GridviewFormattedNoAlt ID="gvPendingRepairBids" runat="server" AllowSorting="true"
                    AllowPaging="true" AutoGenerateColumns="false" Width="600px" 
        onpageindexchanging="gvPendingRepairBids_PageIndexChanging">
                    <Columns>
                        <asp:TemplateField HeaderText="Contractor Name:" SortExpression="BidCompany">
                            <ItemTemplate>
                                <asp:Label ID="lblBidCompany" runat="server" Text='<%# Eval("BidCompany") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Estimated Completion Days:" SortExpression="RprEstCompletionDays">
                            <ItemTemplate>
                                <asp:Label ID="lblEstimatedCompletionDays" runat="server" Text='<%# Eval("RprEstCompletionDays") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Bid Total:" SortExpression="sumofamt">
                            <ItemTemplate>
                                <asp:Label ID="lblBidTotal" runat="server" Text='<%# Eval("sumofamt") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Bid Document:" SortExpression="BidDocID">
                            <ItemTemplate>
                                <a id="linkViewDocument" runat="server" href='<%# "~/ViewDoc.Aspx?DocID=" + Eval("BidDocID") + "-" + Eval("BidDocSID") %>'>
                                    PDF</a>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Bid Details:" SortExpression="BidDocID">
                            <ItemTemplate>                                
                                <a id="linkViewDocumentFAKE" runat="server" href='<%# "~/Main.aspx?MenuId=537&AssetTaskID=" + Eval("AssetTaskID")  + "-" + Eval("AssetTaskSID") %>'>View</a>
                                
                            </ItemTemplate>
                        </asp:TemplateField>
                         <asp:TemplateField HeaderText="Bid Status:" SortExpression="StatusID">
                            <ItemTemplate>                                
                                <cc1:DdlLookup runat="server" AllowUnassigned="true" HeaderCode="PresBidSta" DataTextField="Description" AllowAll="true" DataValueField="StatusID"></cc1:DdlLookup>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </cc1:GridviewFormattedNoAlt>

Open in new window



Here is how the grid view is FILLED initially:

    using (MarketingDataContext dc2 = new MarketingDataContext())
            {
                _pendingRepairBids = dc2.Marketing_Plan_PendingRepairBids_Get(atid, atsid).ToList<Marketing_Plan_PendingRepairBids_GetResult>();
                this.gvPendingRepairBids.DataSource = _pendingRepairBids;
                this.gvPendingRepairBids.DataBind();
            }

Open in new window




I was beginning to write the UPDATE part  ( which is what I am asking about )

Here is as far as I got:

//Update RepairBidStatus
            using(MarketingDataContext mdc = new MarketingDataContext)
            {
                int repairBidID;
                int repairBidSID;
                int statusID;
                mdc.Marketing_Plan_PendingRepairBids_UpdateStatus(repairBidID, repairBidSID, statusID);
            }

Open in new window



Do I need to loop through the rows of the grid view?

Or is there some special LINQ way to do this?





[[EDIT]]


This compiles okay, but I don't want to hard-code the column indexes!

    //Update RepairBidStatus
     using(MarketingDataContext mdc = new MarketingDataContext())
            {
                int repairBidID = 0;
                int repairBidSID = 0;
                int statusID = 0;
                foreach(GridViewRow gvr in gvPendingRepairBids.Rows)
                {
                    repairBidID = Convert.ToInt32(gvr.Cells[7].Text);
                    repairBidSID = Convert.ToInt32(gvr.Cells[8].Text);
                    statusID = Convert.ToInt32(gvr.Cells[9].Text);
                    mdc.Marketing_Plan_PendingRepairBids_UpdateStatus(repairBidID, repairBidSID, statusID);
                }

Open in new window

0
Comment
Question by:Tom Knowlton
[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
2 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 38842523
I don't think there's a better way to do it other than the indexes unless you have something like this

<asp:TemplateField HeaderText="Bid ID:" SortExpression="repairBidID">
                            <ItemTemplate>
                                <asp:Label ID="lblBidID" runat="server" Text='<%# Eval("repairBidID") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>

Open in new window


Then you could use

foreach(GridViewRow gvr in gvPendingRepairBids.Rows)
                {
                    repairBidID = Convert.ToInt32(((Label)gvr.FindControl("lblBidID"))).Text);
                 }

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 38844009
Thank you for your reply on this.


This is what I ended-up doing:

  //Update RepairBidStatus
            using(MarketingDataContext mdc = new MarketingDataContext())
            {
                int repairBidID = 0;
                int repairBidSID = 0;
                int? statusID = 0;

                foreach(GridViewRow gvr in gvPendingRepairBids.Rows)
                {

                    repairBidID = Convert.ToInt32(((Label)gvr.FindControl("lblRepairBidID")).Text);
                    repairBidSID = Convert.ToInt32(((Label)gvr.FindControl("lblRepairBidSID")).Text);
                    var s = ((Framework.UIControls.DdlLookup)gvr.FindControl("ddlStatus")).LookupID;
                    statusID = Convert.ToInt32(s);

                    if (statusID != 0)
                    {
                        mdc.Marketing_Plan_PendingRepairBids_UpdateStatus(repairBidID, repairBidSID, statusID);
                    }
                    else
                    {
                        mdc.Marketing_Plan_PendingRepairBids_UpdateStatus(repairBidID, repairBidSID, null);
                    }
                }
            } 

Open in new window

0

Featured Post

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!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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