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

                                                Download

                                   DEMO





                      HTML CODING



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    </head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table><tr><td></td><td> 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" style="width: 257px">
            <Columns>
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">GET</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblCity" runat="server" Text='<%# Eval("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("country") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <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>
                    </td></tr></table>

                            </td></tr>
</table>
    </div>
    </form>
</body>


</html>


                         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);
     
   con.Open();

    }

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

            bind1();
       
        }
    }
    protected void bind1()
    {
        dbcon();
        query = "select * from grid";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            // 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.








0 comments:

Post a Comment