Autogenerate Select Button and SelectedIndexChanging Event in GridView ASP.Net

In my previous article I showed How to Bind Data in GridView. This article shows how to use the SelectedIndexChanging Event for selecting data from a GridView and displaying it in a TextBox.
Let's Begin. Set the AutoGenerateSelectButton property of the GridView to true and add a SelectedIndexChanging Event to the GridView. Drop 4 TextBoxes from the toolbox for displaying the value from the GridView on clicking the select button in GridView.
Default.aspx Code
  1. <form id="form1" runat="server">  
  2.     <div>  
  3.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" AutoGenerateSelectButton="true" OnSelectedIndexChanging="GridView1_SelectedIndexChanged1">  
  4.             <Columns>  
  5.                 <asp:BoundField DataField="Id" HeaderText="Emp ID"/>  
  6.                 <asp:BoundField DataField="First Name" HeaderText="First Name"/>  
  7.                 <asp:BoundField DataField="Last Name" HeaderText="Last Name"/>  
  8.                 <asp:BoundField DataField="City" HeaderText="City"/>  
  9.             </Columns>  
  10.             <HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>  
  11.             <RowStyle BackColor="#e7ceb6"/>  
  12.         </asp:GridView>  
  13.         <br />  
  14.         <br />  
  15.         <div style="border:1px solid black; width:336px;">  
  16.   
  17.             <table class="auto-style1">  
  18.                 <tr>  
  19.                     <td class="auto-style5">Emp ID</td>  
  20.                     <td class="auto-style3">  
  21.                         <asp:TextBox ID="txt_ID" runat="server" ReadOnly="True"></asp:TextBox>  
  22.                     </td>  
  23.                 </tr>  
  24.                 <tr>  
  25.                     <td class="auto-style2">First Name</td>  
  26.                     <td class="auto-style4">  
  27.                         <asp:TextBox ID="txt_FN" runat="server"></asp:TextBox>  
  28.                     </td>  
  29.                 </tr>  
  30.                 <tr>  
  31.                     <td class="auto-style5">Last Name</td>  
  32.                     <td class="auto-style3">  
  33.                         <asp:TextBox ID="txt_LN" runat="server"></asp:TextBox>  
  34.                     </td>  
  35.                 </tr>  
  36.                 <tr>  
  37.                     <td class="auto-style2">City</td>  
  38.                     <td class="auto-style4">  
  39.                         <asp:TextBox ID="txt_CT" runat="server"></asp:TextBox>  
  40.                     </td>  
  41.                 </tr>  
  42.                 <tr>  
  43.                     <td class="auto-style5"> </td>  
  44.                     <td class="auto-style3">  
  45.                         <asp:Button ID="Button1" runat="server" Text="Update" />  
  46.                     </td>  
  47.                 </tr>  
  48.             </table>  
  49.   
  50.         </div>  
  51.     </div>  
  52. </form>  
The SelectedIndexChanging event occurs before the GridView control does 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 the TextBox.
 
 Default.aspx.cs Code
  1. using System;  
  2. using System.Web.UI.WebControls;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6.   
  7. public partial class _Default : System.Web.UI.Page  
  8. {  
  9.     protected void Page_Load(object sender, EventArgs e)  
  10.     {  
  11.         if(!IsPostBack)  
  12.         {  
  13.             DisplayData();  
  14.         }  
  15.     }  
  16.     protected void DisplayData()  
  17.     {  
  18.         DataTable dt = new DataTable();  
  19.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);  
  20.         SqlDataAdapter adapt = new SqlDataAdapter("select * from Employee",con);  
  21.         con.Open();  
  22.         adapt.Fill(dt);  
  23.         con.Close();  
  24.         if(dt.Rows.Count>0)  
  25.         {  
  26.             GridView1.DataSource = dt;  
  27.             GridView1.DataBind();  
  28.         }  
  29.     }  
  30.     protected void GridView1_SelectedIndexChanged1(object sender, GridViewSelectEventArgs e)  
  31.     {  
  32.         txt_ID.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;  
  33.         txt_FN.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;  
  34.         txt_LN.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;  
  35.         txt_CT.Text = GridView1.Rows[e.NewSelectedIndex].Cells[4].Text;  
  36.     }  
  37. }  
Final Preview: 
 
I hope you like it. Thanks. 
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all