Solved

Trying to read a value of specific cell in gridview

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

16 Experts available now in Live!

Get 1:1 Help Now