Solved

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

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

Technology Partners: 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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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