Solved

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

Posted on 2013-06-17
10
441 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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