How do I get the SelectedIndex of a GridView when clicking a LinkButton in an ItemTemplate?

My GridView looks like this:
        <asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" AllowPaging="True" >
            <Columns>            
                <asp:BoundField DataField="PHONEID" Visible="False"/>
                <asp:BoundField DataField="PHONENUM" HeaderText="Phone Number" />
                <asp:BoundField DataField="PHONE_LABEL" HeaderText="Label"  />
                <asp:BoundField DataField="PRIMARY_PHONE" HeaderText="Primary" />
                <asp:BoundField DataField="LID" Visible="False"/>
                <asp:TemplateField ShowHeader="false">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="false" Text="Edit" OnClick="lnkEdit_Click()" />
                    </ItemTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton  ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                             OnClientClick='return confirm("Are you sure you want to delete this phone?");' Text="Delete" CommandArgument="x" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle CssClass="pager" />
        </asp:GridView>

In the OnClick event I want to call a procedure that takes the selected row and populates the data into textboxes for edits.  I'm not quite sure how to get the index of the row that was selected when the LinkButton is clicked.  I don't want to edit the data directly on the GridView.
nightshadzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Anurag ThakurTechnical ManagerCommented:
use the DataKeyNames property of the gridveiw and from that you can get the ID of the row - Phone ID in this case
http://www.codeproject.com/KB/webforms/GridViewConfirmDelete.aspx
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
nightshadzAuthor Commented:
I'm not sure how to implement the example in that link you posted.  How would I use DataKeyNames if I don't yet know the row index?  Could you provide an example please?
0
 
nightshadzAuthor Commented:
Wait!  Don't answer yet!  I set CommandArgument='<%# Eval("PHONEID") %>' in the linkbutton.  I'll catch this in the RowCommand event.  If I'm on the right track let me know! =)
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Anurag ThakurTechnical ManagerCommented:
if you look at the sample you will find everything in it
please at least go through the sampe attached and if there are any further issues i will be glad to help
0
 
Anurag ThakurTechnical ManagerCommented:
yes that might also work
0
 
nightshadzAuthor Commented:
I'm a little stumped.  My link button now looks like: <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="false" Text="Edit" CommandArgument='<%# Eval("PHONEID") %>' CommandName="EditPhone" />

In the myGridView_RowCommand I can get the PHONEID but how do I then get the selected row index based on this value?  Do I have to pass in all the other fields I need via the CommandArgument (doesn't seem like the best solution)?
0
 
Anurag ThakurTechnical ManagerCommented:
wont phone id get you the record from the data source itslef?
0
 
nightshadzAuthor Commented:
My RowCommand event looks something like:

        If e.CommandName = "EditPhone" Then
            Try
                Dim dv As New DataView(Me._myDT)
                dv.RowFilter = "PHONEID = " & e.CommandArgument.ToString
                If dv.Count = 0 Then
                   'Not Found
                Else
                   'Found
                End If
            ......

This seems to do the trick.  If you think there is a better way or if there is something wrong with this code please let me know.  Thanks!
0
 
nightshadzAuthor Commented:
This seems to work better:
Dim dr() As DataRow = Me._myDT.Select("PHONEID = " & e.CommandArgument.ToString)

Thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.