Solved

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

Posted on 2013-01-31
2
219 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 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: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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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