Solved

Trying to read a value of specific cell in gridview

Posted on 2013-05-16
2
596 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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