Thursday 16 May 2013

Selecting a Row in Gridview

Selecting a Row in Gridview

http://www.dotnetspark.com/kb/651-selecting-row-gridview.aspx


Add one Gridview and bind a Data using SqlDataSource.and Add three Textboxes to display a data in Textboxes when the GridviewRow is Selected.
Ste the Gridview AutoGenerateColumns property to 'False'.
You will need to set the Bound column in order to see the text in the cells of the grid view control

<asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AutoGenerateSelectButton="True"OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" style="z-index: 100; left: 266px; position: absolute; top: 71px" CellPadding="4"ForeColor="#333333" GridLines="None" Width="306px" Height="137px">
    <Columns >
    <asp:BoundField HeaderText ="Rowid"  DataField ="Rowid" />
    <asp:BoundField HeaderText ="Name" DataField ="Name" />
    <asp:BoundField HeaderText ="Marks" DataField ="marks" />  
    Columns>
asp:GridView>
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>" SelectCommand="SELECT * FROM [emp]">
asp:SqlDataSource>
In Gridview Properties set 'AutoGenerateSelectButton=True'.See below  image of Gridview after setting   'AutoGenerateSelectButton=True'.
 
See th Desing view of your Gridview.you will find a Hyperlink with a text as 'Select'

Selecting Row:

Selecting row event is fired when you make a click on the select link. If you need any particular item in that row you can easily select it using the cells property:
In Gridview Events double Click on SelectedIndexChanged Event and srite below code:
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {
       txtrowid .Text  = Gridview1.SelectedRow.Cells[1].Text;
       txtname .Text  = Gridview1.SelectedRow.Cells[2].Text;
       txtmarks .Text  = Gridview1.SelectedRow.Cells[3].Text;

              
    }



Complete .asp source code:

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





   


   
   

    
    
    
    
       

    

        
        
        
        
        
        
        
   

                    SelectCommand="SELECT * FROM [emp]">
       

       

        
        
        
        
        
        
   
   

   




In .aspx.cs code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {
       txtrowid .Text  = Gridview1.SelectedRow.Cells[1].Text;
       txtname .Text  = Gridview1.SelectedRow.Cells[2].Text;
       txtmarks .Text  = Gridview1.SelectedRow.Cells[3].Text;

              
    }
}

No comments:

Post a Comment