Solved

Trying to read a value of specific cell in gridview

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

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 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