Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How would I get the productIDs out of  a Gridview on page load

Posted on 2013-06-17
10
Medium Priority
?
446 Views
Last Modified: 2013-06-18
I have a Gridview grid that I want the productID out of.  How would I do this?
0
Comment
Question by:bmanmike39
[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
  • 7
  • 2
10 Comments
 
LVL 11

Expert Comment

by:Guru Ji
ID: 39254500
To access a value of a column you can use SelectedRow:

GridViewRow row = GridView1.SelectedRow;
lblMyLabel.Text = row.Cells[2].Text;
0
 

Author Comment

by:bmanmike39
ID: 39254559
I get an error "object not set to an instance"
0
 
LVL 11

Expert Comment

by:Guru Ji
ID: 39254852
Can u post your code where exactly you trying to get the value
0
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 

Author Comment

by:bmanmike39
ID: 39255101
This is the Grid  (Gridview4) Asp Page
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="ProductID,shipZoneID" DataSourceID="SqlDataSource4" 
                       Width="620px" GridLines="None" ShowHeader="False" oninit="GridView4_Init" 
                       onload="GridView4_Load" onprerender="GridView4_PreRender" 
                       onrowdatabound="GridView4_RowDataBound">
                    <RowStyle VerticalAlign="Top" />
                    <Columns>
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("pic") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Image ID="Image1" runat="server" Height="217px" 
                                    ImageUrl='<%# Eval("pic") %>' Width="150px" />
                                <br />
                                CatalogID:
                                <asp:Label ID="Label16" runat="server" Font-Bold="True" 
                                    Text='<%# Eval("productNumber") %>'></asp:Label>
                                <br />
                                <br />.
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Top" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ds">
                            <ItemTemplate>
                                <p>
                                
                                    <b>
                                    <asp:Label ID="Label17" runat="server" CssClass="paddingLeft" Font-Bold="True" 
                                        ForeColor="Maroon" Text='<%# Eval("productName") %>' Width="400px"></asp:Label>
                                    </b>
                                    <br />
                                    <br />
                                    <asp:Label ID="Label18" runat="server" CssClass="paddingLeft" 
                                        Text='<%# GetTruncatedString (Eval("description").ToString()) %>' Width="500px"></asp:Label>
                                    <br />
                                <p class="GridSpacing">
                                    <asp:Label ID="Label19" runat="server" CssClass="paddingLeft" Font-Bold="True" 
                                        Text='<%# Eval("price", "{0:C}") %>'></asp:Label>
                                    &nbsp;&nbsp; Price<br />
                                    <br />
                                    <asp:Label ID="Label20" runat="server" CssClass="paddingLeft" Font-Bold="True" 
                                        Text='<%# Eval("shippingAmount", "{0:C}") %>'></asp:Label>
                                    &nbsp;&nbsp;&nbsp;&nbsp; Cost of Shipping<br />
                                    <br />
                                    <asp:Label ID="Label21" runat="server" CssClass="overLine" Font-Bold="True" 
                                        Text='<%# Eval("Tprice", "{0:C}") %>'></asp:Label>
                                    &nbsp;&nbsp; <b>Total</b>
                                </p>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                            InsertVisible="False" ReadOnly="True" SortExpression="ProductID" >
                        <ItemStyle ForeColor="White" Width="5px" />
                        </asp:BoundField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="Button2" runat="server" onclick="Button2_Click1" 
                                    Text="Add to Cart" />
                                <br />
                                <br />
                                <br />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Bottom" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

Open in new window


This is the code Behind

page load event
protected void Page_Load(object sender, EventArgs e)
    {
        #region handle first item
        // Set session vars on this page 

        if (Session["isFirstItem"] == null)
        {
            itemNumber.Text = "0";
        }
        
        if (itemNumber.Text == "0")
        {
            itemNumber.Text = "1";
            Session["isFirstItem"] = itemNumber.Text;
        }
        // Get the Session zone mumber        
        if (Session["isFirstItem"] == "2")
        {
            itemNumber.Text = "2";
            lbShipZone1.Text = (string)(Session["zoneNumberSession"]);
        }
                    
       
        #endregion
}

Open in new window

0
 

Author Comment

by:bmanmike39
ID: 39255106
Sorry this is the cod behind;

protected void Page_Load(object sender, EventArgs e)
    {
        #region handle first item
        // Set session vars on this page 

        if (Session["isFirstItem"] == null)
        {
            itemNumber.Text = "0";
        }
        
        if (itemNumber.Text == "0")
        {
            itemNumber.Text = "1";
            Session["isFirstItem"] = itemNumber.Text;
        }
        // Get the Session zone mumber        
        if (Session["isFirstItem"] == "2")
        {
            itemNumber.Text = "2";
            lbShipZone1.Text = (string)(Session["zoneNumberSession"]);
        }
                    
       
GridViewRow row = GridView4.SelectedRow;
            Label7.Text = row.Cells[0].Text;

        #endregion
}

Open in new window

0
 

Author Comment

by:bmanmike39
ID: 39255225
I've tried to put this code:
GridViewRow row = GridView4.SelectedRow;
Label7.Text = row.Cells[0].Text;

 in the following events
Pageload
Gridview load
Gridview init
0
 
LVL 4

Accepted Solution

by:
AnuTiji earned 2000 total points
ID: 39255389
Hi

You have set the DataKeyName for the gridview as the ProductID. The value for the Product Ids can be retrieved in the page load method as

 for (int iRow = 0; iRow> GridView4.Rows.Count; iRow++)
    {
              int iProductId=  Convert.ToInt32(GridView4.DataKeys[iRow ].Values[0]);
     }
0
 

Author Comment

by:bmanmike39
ID: 39256697
I have put the following code in the pageload Event, but got nothing.  the label 7 stays empty

code
for (int iRow = 0; iRow > GridView4.Rows.Count; iRow++)
        {
            int iProductId = Convert.ToInt32(GridView4.DataKeys[iRow].Values[0]);
            Label7.Text = iProductId.ToString();
0
 

Author Comment

by:bmanmike39
ID: 39257040
All I'm Trying to do is get the ProductID out of A gridview control and into a Variable as the page Loads.

I can do it on a button click but can't figure it out how to do it in page load.
0
 

Author Comment

by:bmanmike39
ID: 39257086
Got it.

Had to put this in the rowCreated event;
foreach (GridViewRow myRow in GridView4.Rows)
        {
            string decimalString = myRow.Cells[2].Text;
            Label7.Text = decimalString;
        }
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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