Solved

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

Posted on 2013-01-31
2
204 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:knowlton
2 Comments
 
LVL 20

Accepted Solution

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

17 Experts available now in Live!

Get 1:1 Help Now