Monday, February 16, 2009

GridView Control - select without select button

CSS Code:
/* For Hiding Select Column of GridView control*/
.HiddenColumn
{display:none;}


HTML Code:
1 <asp:GridView ID="gvUser" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" Width="880px" PageSize="20" OnSelectedIndexChanged="gvUser_SelectedIndexChanged" HorizontalAlign="Center" CssClass="form" OnRowDataBound="gvUser_RowDataBound">
2 <Columns>
3 <asp:CommandField ShowSelectButton="True">
4 <HeaderStyle CssClass="HiddenColumn" />
5 <ItemStyle CssClass="HiddenColumn" />
6 </asp:CommandField>
7 <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
8 SortExpression="id" />
9 <asp:BoundField DataField="defirst" HeaderText="defirst" SortExpression="defirst" />
10 <asp:BoundField DataField="delast" HeaderText="delast" SortExpression="delast" />
11 <asp:BoundField DataField="deemail" HeaderText="deemail" SortExpression="deemail" />
12 </Columns>
13 <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
14 <HeaderStyle ForeColor="White" />
15 </asp:GridView>

C# Behind Code:
protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add(
"onmouseover", "this.style.cursor = 'hand'; this.style.backgroundColor = '#FFCCFF';");
e.Row.Attributes.Add(
"onmouseout", "this.style.backgroundColor='';");
e.Row.Attributes[
"onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvUser, "Select$" + e.Row.RowIndex);
}
}