Solved

How to access the selectedrow and button filed in gridview row command.

Posted on 2013-01-07
2
398 Views
Last Modified: 2013-01-07
<asp:GridView ID="grd1" runat="server" SkinID="GridView10RowsSortAndPage" OnRowCommand="Grid_RowCommand"
                                                                            OnRowDataBound="Grid_RowDataBound">
                                                                            <Columns>
                                                                                <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
                                                                               <asp:ButtonField  runat="server" ButtonType="Link" headerText="Amount" datatextfield="AMOUNT" CommandName="Select" />
                                                                                <asp:TemplateField HeaderText="Type">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lblDesc" runat="server" Text='<%# Bind("DESCRIPTION")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                 
                                                                                </asp:TemplateField>
                                                                                <asp:TemplateField HeaderText="Date">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lbldate" runat="server" Text='<%# Bind("Date")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                 
                                                                                </asp:TemplateField>
                                                                                <asp:TemplateField HeaderText="Time">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lbltime" runat="server" Text='<%# Bind("TIME")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                 
                                                                                </asp:TemplateField>
                                                                               
                                                                                <asp:TemplateField HeaderText="Name">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lblname" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                   
                                                                                </asp:TemplateField>
                                                                            </Columns>
                                                                        </asp:GridView>



        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)

        {

            GridView grd = (GridView)sender;

            try

            {

                int iRowIndex = Convert.ToInt32(e.CommandArgument);

                switch (grd.ID.ToString())

                {

                   
                    case "grd1":

                        {

                            int index = Convert.ToInt32(e.CommandArgument);
                                LinkButton lnkAmt = new LinkButton();
                                lnkAmt = (LinkButton)sender;

                                GridViewRow gvr = lnkAmt.NamingContainer as GridViewRow;
                                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                           
                            break;

                        }

                }

            }

            catch (Exception ex)

            {

                string s = ex.Message;

            }

            finally

            {

               

            }

        }
 

How to access the selected row in gridview row command and button filed in the selected row.I tried to access selected  gridview row by 2 ways. But nothing worked out.

Please let me know.
Thanks
0
Comment
Question by:KavyaVS
[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 15

Accepted Solution

by:
navneethegde earned 500 total points
ID: 38752589
Hi!

You looking for this

   <asp:GridView ID="grd1" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" runat="server" >
        <Columns>
            <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
            <asp:ButtonField runat="server" ButtonType="Link" HeaderText="Amount" DataTextField="AMOUNT"
                CommandName="Select" />
            <asp:TemplateField HeaderText="Type">
                <ItemTemplate>
                    <asp:Label ID="lblDesc" runat="server" Text="DESCRIPTION"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date">
                <ItemTemplate>
                    <asp:Label ID="lbldate" runat="server" Text="Date"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Time">
                <ItemTemplate>
                    <asp:Label ID="lbltime" runat="server" Text="TIME"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="lblname" runat="server" Text="Name"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window


        protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String output = grd1.SelectedRow.Cells[1].Text;
        }

Open in new window



Thanks!
0
 

Author Closing Comment

by:KavyaVS
ID: 38752698
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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