.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Selecting a Row in Gridview

Posted By:Syed Shakeer Hussain       Posted Date: June 21, 2009    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  
 

Selecting a Row in Gridview


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" %>





    Untitled Page


   
   

   
   
   
   
      

   

       
       
       
       
       
       
       
   

                    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;

              
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend