In this code sample we have a Check box column in a child GridView control in ASP.NET. On clicking the textbox's header the entire row of the grid column should get selected.
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; 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 NestedGridView1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("<Connection String>"); conn.Open();
DataTable dt = new DataTable(); SqlDataAdapter ADA = new SqlDataAdapter("<Select Query for Parent Grid>", conn); ADA.Fill(dt); grd.DataSource = dt; grd.DataBind(); } protected void OnDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GridView Grd = (GridView)e.Row.FindControl("ChldGrid"); SqlConnection conn = new SqlConnection("<Connection String for Child Grid>"); conn.Open(); DataTable dt = new DataTable("TEST"); SqlDataAdapter ADA = new SqlDataAdapter("<Select Query for Child Grid> where <Column Name> = " + e.Row.Cells["<Cell Index>"].Text, conn); ADA.Fill(dt); Grd.DataSource = dt; Grd.DataBind(); } } protected void OnChidDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { CheckBox chk = (CheckBox)e.Row.FindControl("chkHeader"); chk.Attributes.Add("onclick", "javascript:SelectAll('" + chk.ClientID + "')"); } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NestedGridView1.aspx.cs" Inherits="NestedGridView1" %>
<!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> <script type="text/javascript"> function SelectAll(ID) { var chk = document.getElementById(ID); var chks = document.form1.elements; for (i=0; i<chks.length; i++) { var type = chks[i].type; if (type=="checkbox") if (chk.checked) { chks[i].checked = true; } else chks[i].checked = false; } } </script> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="grd" AutoGenerateColumns="false" OnRowDataBound="OnDataBound"> <Columns> <asp:BoundField DataField="<Column Name>" HeaderText="Header Text" /> <asp:TemplateField HeaderText="Child Grid"> <ItemTemplate> <asp:GridView runat="server" ID="ChldGrid" AutoGenerateColumns="false" OnRowDataBound="OnChidDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox runat='server' ID="chk" /> </ItemTemplate> <HeaderTemplate> <asp:CheckBox runat='server' ID="chkHeader" /> </HeaderTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Header Text" DataField="<Column Name>" /> </Columns> </asp:GridView> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
AttachmentsNested GridView Design (19019-2060-NestedGridView1.aspx.txt)Code Behind File (19019-2061-NestedGridView1.aspx.cs.txt)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|