Solved

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

Posted on 2013-06-17
10
444 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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