Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday, 17 August 2014

Autogenerate Select button and SelectedIndexChanging Event in GridView Asp.NET

In my previous blog, I have shown How to Bind Data in Gridview. In this article, I am going to show How to use SelectedIndexChanging Event for Selecting data from Gridview and displaying it in TextBox.

Let's Begin:
Set AutoGenerateSelectButton property of Gridview to true and add SelectedIndexChanging Event of Gridview. Drop 4 TextBox from the toolbox for displaying the value from the GridView on Clicking the select button in Gridview.

Default.aspx Code:

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" AutoGenerateSelectButton="true" OnSelectedIndexChanging="GridView1_SelectedIndexChanged1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Emp ID"/>
                <asp:BoundField DataField="First Name" HeaderText="First Name"/>
                <asp:BoundField DataField="Last Name" HeaderText="Last Name"/>
                <asp:BoundField DataField="City" HeaderText="City"/>
            </Columns>
            <HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>
            <RowStyle BackColor="#e7ceb6"/>
        </asp:GridView>
        <br />
        <br />
        <div style="border:1px solid black; width:336px;">

            <table class="auto-style1">
                <tr>
                    <td class="auto-style5">Emp ID</td>
                    <td class="auto-style3">
                        <asp:TextBox ID="txt_ID" runat="server" ReadOnly="True"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">First Name</td>
                    <td class="auto-style4">
                        <asp:TextBox ID="txt_FN" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style5">Last Name</td>
                    <td class="auto-style3">
                        <asp:TextBox ID="txt_LN" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">City</td>
                    <td class="auto-style4">
                        <asp:TextBox ID="txt_CT" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style5">&nbsp;</td>
                    <td class="auto-style3">
                        <asp:Button ID="Button1" runat="server" Text="Update" />
                    </td>
                </tr>
            </table>

        </div>
    </div>
</form>
SelectedIndexChanging event occurs before the GridView control performs the select operation so, the SelectedIndex property of the control cannot be used to determine the index of the new row selected by the user. To determine the index of the new row selected by the user, use the NewSelectedIndex property and display the value in TextBox.

Default.aspx.cs Code:
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            DisplayData();
        }
    }
    protected void DisplayData()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
        SqlDataAdapter adapt = new SqlDataAdapter("select * from Employee",con);
        con.Open();
        adapt.Fill(dt);
        con.Close();
        if(dt.Rows.Count>0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    protected void GridView1_SelectedIndexChanged1(object sender, GridViewSelectEventArgs e)
    {
        txt_ID.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
        txt_FN.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
        txt_LN.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
        txt_CT.Text = GridView1.Rows[e.NewSelectedIndex].Cells[4].Text;
    }
}
Final Preview:
Hope you Like it. Thanks.
[Download Source Code via Google Drive]

Wednesday, 13 August 2014

DataBinding in GridView

In this Article, We are going to see a simple example of DataBinding in GridView. For Demonstration, I have created a database (named EmployeeDB) in which We have a table named Employee.

Let's Begin:
1) Drop GridView Control from the toolbox and set AutoGenerateColumns to false
2) Add Columns Collection (tag) which manage the collection of Column fields.
3) Inside Columns tag, Add column field (BoundField) which displays the value of a field in a data source.
4) Set the DataField property to the name of column in table for binding to the BoundField object and Set HeaderText value for Displaying it on the GridView's Header. 
5) We can also adjust the appearance of the Header and Row in a GridView using <HeaderStyle> element and <RowStyle> element.

Default.aspx Coding:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="6">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Employee ID" />
                <asp:BoundField DataField="First Name" HeaderText="First Name" />
                <asp:BoundField DataField="Last Name" HeaderText="Last Name" />
                <asp:BoundField DataField="City" HeaderText="City" />
            </Columns>
            <HeaderStyle BackColor="#0066cc" Font-Bold="true" ForeColor="White" />
            <RowStyle BackColor="#bfdfff" ForeColor="Black" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

In this Example, I am using ADO.NET for Binding the data to GridView. For this We have to add System.Data and System.Data.SqlClient namespace in the namespace declaration.

Default.aspx.cs Code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ShowData();
        }
    }
    //getting Connection String from Web.config file
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    //Method for DataBinding
    protected void ShowData()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(cs);
        SqlDataAdapter adapt = new SqlDataAdapter("select * from Employee",con);
        con.Open();
        adapt.Fill(dt);
        con.Close();
        if(dt.Rows.Count>0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}
Final Preview:
Hope you like it. Thanks.
[Download Source Code from Google Drive]

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook