GridView SelectedIndexChanged Use in Asp.Net C#

GridView SelectedIndexChanged 

Gridview DoubleClick Event Use in the Condition Used . Gridview Row Values Pass to Label Box Using  SelectCommand in LinkButton.

                           Download Coding



                      HTML CODING

<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" style="width: 257px">
                <asp:TemplateField HeaderText="Action">
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">GET</asp:LinkButton>
                <asp:TemplateField HeaderText="ID">
                        <asp:Label ID="lblID" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                <asp:TemplateField HeaderText="City">
                        <asp:Label ID="lblCity" runat="server" Text='<%# Eval("city") %>'></asp:Label>
                <asp:TemplateField HeaderText="Country">
                        <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("country") %>'></asp:Label>
            <HeaderStyle BackColor="Red" />
            <RowStyle BackColor="#FFCC99" />
        </asp:GridView></td><td class="auto-style1"><table style="width: 176px; height: 71px"><tr><td class="auto-style3">
                <asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
                </td><td class="auto-style4">
                    <asp:Label ID="lblGetID" runat="server" ForeColor="#FF9900" Text="Label"></asp:Label>
                </td></tr><tr><td class="auto-style3">
                    <asp:Label ID="Label2" runat="server" Text="City"></asp:Label>
                    </td><td class="auto-style4">
                        <asp:Label ID="lblGetCity" runat="server" ForeColor="#FF9900" Text="Label"></asp:Label>
                    </td></tr><tr><td class="auto-style3">
                    <asp:Label ID="Label3" runat="server" Text="Country"></asp:Label>
                    </td><td class="auto-style4">
                        <asp:Label ID="lblGetCountry" runat="server" ForeColor="#FF9900" Text="Label"></asp:Label>



                         C# CODING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class GridSelectIndex : System.Web.UI.Page
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

    public void dbcon()
  string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
con = new SqlConnection(connn);


    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    protected void bind1()
        query = "select * from grid";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
            GridView1.DataSource = ds;
            GridView1.DataSource = ds;
            int columncount = GridView1.Rows[0].Cells.Count;
            // GridView1.FooterRow.Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

        Label id = (Label)GridView1.SelectedRow.Cells[1].FindControl("lblID");
        lblGetID.Text = id.Text.ToString();

        Label city = (Label)GridView1.SelectedRow.Cells[2].FindControl("lblCity");
        lblGetCity.Text = city.Text.ToString();

        Label country = (Label)GridView1.SelectedRow.Cells[2].FindControl("lblCountry");
        lblGetCountry.Text = country.Text.ToString();



First Add New WebForm - Select GridView From ToolBox

Next - Create DatBase Table 

Next - Right Click GridView - Select Edit Column

Add the TemplateField - Change Header Text  (Required)

Next - Change All TemplateField  Header  - OK

Next - GridView  Add Row Colour - Header Colour  & Right Click GridView  - Goto Edit Template.

Next - Go To Action Column - Add LinkButton - Change CommandName=Select 

Next - Go to ID Column - Add Label - Edit Data Binding - Bind Table Field  - Change Label ID

Next - Go to City Column - Add Label - Edit Data Binding - Bind Table Field  - Change Label ID

Next - Go to Country Column - Add Label - Edit Data Binding - Bind Table Field  - Change Label ID

Next - DataBounded - Add the Display Labels Change Id.

Next - Insert the Data's to Related Table

Next - DoubleClick - GridView  - Go to Code Behind - Add NameSpaces - DataBase Connections
- Select Table Values to GridView.

Next - SelectedIndexChanged - Select GridView Row Values To Display Labels 

Next - Run the Program [F5]  -  Get Button Click Related Row Values Display To Labels.


Post a Comment