boutique replica bags up ideas

the best replique rolex and prices here.

julia highlight 99j hair color 10a quality straight human hair lace front wigs 8 - 24 inches pre plucked hairline 13x4 inches lace front brazilian wig onlinefor sale

Select Row in ASP.NET GridView Example

Updated on     Kisan Patel

This tutorial will explain you how to select gridview row and display the selected record in ASP.NET with example.

Default.aspx

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" OnSorting="SortRecords" runat="server"
            AllowSorting="True" CellPadding="4" DataKeyNames="Id" 
            AllowPaging="true" OnPageIndexChanging="PaginateGridView" 
            PageSize="5" PagerSettings-Mode="Numeric" 
            SelectedRowStyle-BackColor="Red" AutoGenerateSelectButton="true"
            EnablePersistedSelection="true" />
    </div>
    <asp:Button ID="btnFind" runat="server" Text="Get Selected Record"
        OnClick="GetSelectedRecord" />
 </form>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}

private DataTable GetData()
{
    DataTable table = new DataTable();
    // get the connection
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        // write the sql statement to execute
        string sql = "SELECT Id, FirstName, LastName, Email, IsActive FROM Students";
        // instantiate the command object to fire
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            // get the adapter object and attach the command object to it
            using (SqlDataAdapter sdt = new SqlDataAdapter(cmd))
            {
                // fire Fill method to fetch the data and fill into DataTable
                sdt.Fill(table);
            }
        }
    }

    return table;
}

private void BindData()
{
    // specify the data source for the GridView
    GridView1.DataSource = GetData();
    // bind the data now
    GridView1.DataBind();
}

protected void GetSelectedRecord(object sender, EventArgs e)
{
    var Id = GridView1.SelectedDataKey.Value;
    //Use "Id" to manipulate row...
    Response.Write("Selected record: " + Id);
}

In the above code, we need to set AutoGenerateSelectButton=”true” that generates the Select link column. Clicking the Select link selects the GridView row. After selecting a record, by default when we move to another page, the selection is lost; so in order to persist the selection while navigating to another pages, we can set EnablePersistedSelection=”true”.

To get the selected record, we need to access the SelectedDataKey property of the GridView. Notice that we have done that in the GetSelectedRecord method that fires on the click of the button.

protected void GetSelectedRecord(object sender, EventArgs e)
{
    var Id = GridView1.SelectedDataKey.Value;
    //Use "Id" to manipulate row...
    Response.Write("Selected record: " + Id);
}

Demo

gridview-SelectedRow-demo

Download Complete Source Code


ASP.NET

Leave a Reply