Solved

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

Posted on 2013-06-17
10
443 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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