ASP.Net Gridview Select Row

by top54u.com 18 Jul, 2008

You can select a row of ASP.Net GridView Data Control using postback event of button control or link button control by passing the Command="select". Command property of button or link button control passes the name of command i.e. action to be taken on the clicked row item. When user clicks on any row item of GridView it sends the row index along with command specified for the button control. You can use the GridView TemplateField to display the data items retrieved from the SQL Database table. GridView provides the autoGenerateSelectButton property that accepts true or false. If autoGenerateSelectButton property is set tot true then GridView Control automatically renders the select button at runtime to select the row item. You can also use the GridView CommandField column to handle the GridView Select command. When you specify a command as select for CommandField or autoGenerateSelectButton="true" GridView control automatically handles the event to change the selected Row Index.

In this tutorial for GridView Control Select Row, we have used the Northwind SQL database and GridView is bound with category table.

 

CSS and HTML Code for ASP.Net GridView Select Row Command

 

<style type="text/css">

body {

font-family:arial;

font-size:12px;

}

 

a {

text-decoration:none;

color:#666666;

}

 

.gridHeader th {

border-bottom:solid 1px #444444;

}

 

</style>

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" Width="400px"

SelectedRowStyle-BackColor="#e1e1e1" CellPadding="4" CssClass="gridHeader">

<Columns>

<asp:TemplateField HeaderText="Category">

<ItemTemplate>

<%# DataBinder.Eval(Container.DataItem, "categoryName") %>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Description">

<ItemTemplate>

<%# DataBinder.Eval(Container.DataItem, "description") %>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

</asp:TemplateField>

<asp:CommandField HeaderText="Select" ShowHeader="True" ShowSelectButton="True" >

<HeaderStyle HorizontalAlign="Left" />

</asp:CommandField>

</Columns>

<SelectedRowStyle BackColor="#FFFFBB" Font-Bold="True" />

</asp:GridView>

 

Output:

 

Spotlight.....

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:40:32 AM




related videos.....
recent posts.....
top54u ezines.....