Solved

Trying to read a value of specific cell in gridview

Posted on 2013-05-16
2
607 Views
Last Modified: 2013-05-17
I am trying to loop through a selected row in a gridview and capture a value to pass to a sub that saves the data in a SQL DB.  The problem is my values are returning empty strings.  

Below is my code, I first check a specific column for a checked checkbox and if it is checked I start the save routine.  The checking for a checkbox works fine, it is capturing values that is difficult.  You will see I have hard coded a zero for an integer value so I won't get a converting type error while testing.  There is also a hard coded email address also for testing.

        For Each Row As GridViewRow In gv_advisor.Rows

            If (CType(Row.Cells(6).FindControl("chkUseContact"), CheckBox)).Checked = True Then

                UpdateAdvisorInfo(Row.Cells(1).Text _
                                  , 0 _
                                  , Row.Cells(3).Text _
                                  , Row.Cells(4).Text _
                                  , "email@test.com") 

            End If


        Next

Open in new window


Here is the code for the gridview.  It has 6 data bound columns and 3 additional columns that I have appended including the checkbox column.  The data I am trying to read all comes from databound columns.

 <asp:GridView ID="gv_advisor" visible="true" runat="server" 
                    AutoGenerateColumns="False" BorderWidth="1px" BackColor="White" GridLines="Both"
                    CellPadding="4" BorderStyle="Solid" BorderColor="#DEDFDE" ForeColor="#006699"
                    AllowSorting="False">
                    <FooterStyle BackColor="#006699"></FooterStyle>
                    <PagerStyle ForeColor="White" HorizontalAlign="Right" BackColor="#F7F7DE"></PagerStyle>
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#006699"></HeaderStyle>
                    <AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
                    
                     <Columns>

                        <asp:TemplateField HeaderText="Advisor ID">
                            <ItemTemplate>
                                <asp:Label  runat="server" ID="advisorid" Text='<%# Eval("ADVISOR_ID") %>' />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                
                            <ItemStyle VerticalAlign="Top" />
                        </asp:TemplateField>

                           <asp:TemplateField HeaderText="Plan ID">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="planid" Text='<%# Eval("PLAN_ID") %>' />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                           
                            <ItemStyle VerticalAlign="Top" />
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Plan Name">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="planname" Text='<%# Eval("PLAN_NAME") %>' />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                            
                            <ItemStyle VerticalAlign="Top" />
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="First Name">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="advisorfirst" Text='<%# Eval("First") %>' />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                           
                            <ItemStyle VerticalAlign="Top" />

                        </asp:TemplateField>
                         <asp:TemplateField HeaderText="Last Name">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="advisorlast" Text='<%# Eval("Last") %>' />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                            
                            <ItemStyle VerticalAlign="Top" />
                        </asp:TemplateField>

                           <asp:TemplateField HeaderText="Firm Name">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="firmname" Text='<%# Eval("ADVISOR_FIRM_NAME") %>' />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                       
                            <ItemStyle VerticalAlign="Top" />
                        </asp:TemplateField>

                           <asp:TemplateField HeaderText="Correct As Is">
                            <ItemTemplate>
                                <asp:Checkbox runat="server" ID="chkUseContact" />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                          
                            <ItemStyle VerticalAlign="Middle" />
                        </asp:TemplateField>

                          <asp:TemplateField HeaderText="Alt Contact First">
                            <ItemTemplate>
                                <asp:textbox runat="server" ID="txt_NewContact_First" />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                           
                            <ItemStyle VerticalAlign="Middle" />
                        </asp:TemplateField>

                         <asp:TemplateField HeaderText="Alt Contact Last">
                            <ItemTemplate>
                                <asp:textbox runat="server" ID="txt_NewContact_Last" />
                            </ItemTemplate>
                            <ItemStyle VerticalAlign="Middle" />
                     
                            <ItemStyle VerticalAlign="Middle" />
                        </asp:TemplateField>

                        </Columns> 
                    </asp:GridView>

Open in new window

0
Comment
Question by:pensiongenius
[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
2 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 39173130
I dealt with something similar in a recent project.  Here's how I obtained the values from individual cells in a gridview

    protected void IfRowModifiedUpdate(GridViewRow r)
    {

        //declarations

        //get column values from current gridview row
        bdNo = ((TextBox)(r.Cells[24].Controls[0])).Text;
        ps = ((TextBox)(r.Cells[14].Controls[0])).Text;
        ss = ((TextBox)(r.Cells[15].Controls[0])).Text;
        lgl = ((TextBox)(r.Cells[16].Controls[0])).Text;
        prty = ((TextBox)(r.Cells[17].Controls[0])).Text;
        nts = ((TextBox)(r.Cells[18].Controls[0])).Text;
        ants = ((TextBox)(r.Cells[19].Controls[0])).Text;
        ta = ((TextBox)(r.Cells[23].Controls[0])).Text;


So, instead of Row.Cells(index).Text
try
((TextBox)(Row.Cells[index].Controls[0])).Text

OM Gang
0
 

Author Closing Comment

by:pensiongenius
ID: 39174880
This wasn't the exact solution, but it did get me on the right track.
Also it was c# not vb but that isn't an issue, it was simple enough to translate over.

Here is what ultimately captured the value:

(CType(Row.Cells(1).FindControl("planid"), Label)).Text
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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