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


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 :21/06/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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <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>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <SelectedRowStyle BackColor="Teal" ForeColor="Maroon" Font-Bold="True" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>"
            SelectCommand="SELECT * FROM [emp]"></asp:SqlDataSource>

        <br />
        <br />
        <asp:TextBox ID="txtrowid" runat="server" Style="z-index: 101; left: 365px; position: absolute;
            top: 251px"></asp:TextBox>
        <asp:TextBox ID="txtname" runat="server" Style="z-index: 102; left: 365px; position: absolute;
            top: 283px"></asp:TextBox>
        <asp:TextBox ID="txtmarks" runat="server" Style="z-index: 103; left: 365px; position: absolute;
            top: 315px"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Style="z-index: 107; left: 305px; position: absolute;
            top: 253px" Text="RowId"></asp:Label>
        <asp:Label ID="Label2" runat="server" Style="z-index: 105; left: 303px; position: absolute;
            top: 287px" Text="Name"></asp:Label>
        <asp:Label ID="Label3" runat="server" Style="z-index: 106; left: 306px; position: absolute;
            top: 318px" Text="Marks"></asp:Label>
   
    </div>
    </form>
</body>
</html>


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;

              
    }
}

Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
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    Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend