Wednesday, October 27, 2010

Get Selected row index in gridview

How to  Get current Row Index  in  GridView
in aspx.cs file

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TampilAnggota : System.Web.UI.Page
{
    private string s_kdUser;
    private string s_inisial;

    public string KdUser
    {
        get
        { return s_kdUser; }

        set
        { s_kdUser = value; }
    }

    public string Inisial
    {
        get
        { return s_inisial; }

        set
        { s_inisial = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void anggotaGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);

        if (e.CommandName.Equals("Select"))
        {
            s_kdUser = anggotaGridView.DataKeys[index].Values[0].ToString();
            Server.Transfer("~/Admin/TampilAnggotaDetail.aspx");
        }
        else if(e.CommandName.Equals("Reset"))
        {
            s_inisial = anggotaGridView.DataKeys[index].Values[1].ToString();
            Server.Transfer("~/Admin/ResetPassword.aspx");
        }
    }

    protected void anggotaGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            (e.Row.Cells[0].Controls[0] as WebControl).Attributes.Add("onclick", "return confirm ( 'Anda yakin ?' )");
        }     
    }
}


and aspx file :

<asp:GridView ID="anggotaGridView" runat="server" SkinID="GridViewSkin" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="KdUser,Inisial" DataSourceID="anggotaObjectDataSource" OnRowCommand="anggotaGridView_RowCommand" OnRowDataBound="anggotaGridView_RowDataBound" >
                                <Columns>
                                    <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
                                    <asp:TemplateField ShowHeader="False">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="resetLinkButton" runat="server" CausesValidation="False" CommandName="Reset"
                                                SkinID="GridViewHyperLink" Text="Reset"  CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="KdUser" HeaderText="KdUser" ReadOnly="True" SortExpression="KdUser" />
                                    <asp:BoundField DataField="Inisial" HeaderText="Inisial" SortExpression="Inisial" />
                                    <asp:BoundField DataField="Nama" HeaderText="Nama" SortExpression="Nama" />
                                    <asp:BoundField DataField="Angkatan" HeaderText="Angkatan" SortExpression="Angkatan" />
                                    <asp:BoundField DataField="Aktif" HeaderText="Aktif" SortExpression="Aktif" />
                                </Columns>
                           
                            </asp:GridView>

i using Container.DataItemIndex

No comments:

Post a Comment