?
Solved

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

Posted on 2013-01-31
2
Medium Priority
?
229 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
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 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