Solved

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

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

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now