Gridview Inside a GridView
Description:
Here I am showing Gridview as Child Control in GridView.Write the following in aspx page
Default.aspx:
Here I am showing Gridview as Child Control in GridView.Write the following in aspx page
Default.aspx:
<asp:GridView runat="server" ID="gvEmpdetails" DataKeyNames="EmpID" DataSourceID="EmpDetails"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowDataBound="gvEmpdetails_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkhead" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" SortExpression="EmpSal" />
<asp:BoundField DataField="EmpBranch" HeaderText="EmpBranch" SortExpression="EmpBranch" />
<asp:BoundField DataField="EmpCode" HeaderText="EmpCode" SortExpression="EmpCode" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView runat="server" ID="gvEmpChilddetails"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpTax" HeaderText="EmpTax" SortExpression="EmpTax" />
<asp:BoundField DataField="EmpPF" HeaderText="EmpPF" SortExpression="EmpPF" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx.cs:
First identify the childgridview id in the RowDataboundEvent and then bind the data accordingly based on the condition.Write the below code in the RowDataBoundEvent as follows.
protected void gvEmpdetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
rowindex = e.Row.RowIndex - 1;
if (gvEmpdetails.Rows.Count > 0)
{
Empid = Convert.ToInt16(gvEmpdetails.DataKeys[e.Row.RowIndex-1].Value);
}
if (Empid > 0)
{
//TemplateField temp = (TemplateField)e.Row.
GridView gdChildDetails = (GridView)gvEmpdetails.Rows[rowindex].FindControl("gvEmpChilddetails");
SqlConnection scon = null;
SqlDataAdapter sda = null;
DataSet ds = null;
scon = new SqlConnection(@"Data Source=TAPA-PC;user Id=sa;password=123;DataBase=Customer");
sda = new SqlDataAdapter("select * from EmpDeduction where EmpID=" + Empid, scon);
ds = new DataSet();
sda.Fill(ds, "emp");
if (ds.Tables[0].Rows.Count > 0)
{
gdChildDetails.DataSourceID = null;
gdChildDetails.DataSource = ds;
gdChildDetails.DataBind();
}
else
{
gdChildDetails.DataSourceID = null;
gdChildDetails.DataSource = null;
gdChildDetails.DataBind();
}
}
}
Subscribe to:
Post Comments (Atom)
Labels
- Abstraction in Object Oriented Programming (OOPS) Concept (1)
- Access ChildControls in Gridview using Javascript (1)
- Add a WCF Service Reference to the Client (1)
- ASP.Net GridView Highlight Row onmouseover (1)
- ASP.NET View State And ViewStateEncryptionModes Overview (1)
- Calling Javascript From Any Part Of Code Behind Page By Registering The Script (1)
- Check And UnCheck CheckBoxes In Gridview using javascript (1)
- contact your server administrator. (1)
- DataControlField class (1)
- DataKeys ID (Identity Column) in Child Controls events in GridView (1)
- DataList Paging With PagedDataSource (1)
- Declaring Session in Asp.Net (1)
- Difference between Struct and Class (1)
- Displaying Images In GridView From DataBase (1)
- Dynamic Sitemaps in ASP.NET (1)
- Err: You must install Office SharePoint Server 2007 – Please read Microsoft Knowledge Base article: 962935 with the most recent service pack (1)
- GridView Class (1)
- Gridview Inside GridView (1)
- Gridview Paging using C# (1)
- Gridview Sorting Using C# (1)
- GridView.RowDataBound EventEvent (1)
- GridViewRow (1)
- Inserting Images To Database And Display in GridView (1)
- Microsoft Office Sharepoint Server 2007 on Windows Server 2008 – This Program is blocked due to compatibility issues (1)
- ModalPopUp Using CSS and Div To Reduce the Weight On WebPage (1)
- Session State in Asp.Net OverView (1)
- SharePoint 2010 – The form cannot be rendered. This may be due to a misconfiguration of the Microsoft SharePoint Server State Service. For more information (1)
- Tooltip GridView Header (1)
- Tree View Menu Control (1)
- WCF Service (12)
- What is Encapsulation in OOPS? (1)
Topics
- Abstraction in Object Oriented Programming (OOPS) Concept (1)
- Access ChildControls in Gridview using Javascript (1)
- Add a WCF Service Reference to the Client (1)
- ASP.Net GridView Highlight Row onmouseover (1)
- ASP.NET View State And ViewStateEncryptionModes Overview (1)
- Calling Javascript From Any Part Of Code Behind Page By Registering The Script (1)
- Check And UnCheck CheckBoxes In Gridview using javascript (1)
- contact your server administrator. (1)
- DataControlField class (1)
- DataKeys ID (Identity Column) in Child Controls events in GridView (1)
- DataList Paging With PagedDataSource (1)
- Declaring Session in Asp.Net (1)
- Difference between Struct and Class (1)
- Displaying Images In GridView From DataBase (1)
- Dynamic Sitemaps in ASP.NET (1)
- Err: You must install Office SharePoint Server 2007 – Please read Microsoft Knowledge Base article: 962935 with the most recent service pack (1)
- GridView Class (1)
- Gridview Inside GridView (1)
- Gridview Paging using C# (1)
- Gridview Sorting Using C# (1)
- GridView.RowDataBound EventEvent (1)
- GridViewRow (1)
- Inserting Images To Database And Display in GridView (1)
- Microsoft Office Sharepoint Server 2007 on Windows Server 2008 – This Program is blocked due to compatibility issues (1)
- ModalPopUp Using CSS and Div To Reduce the Weight On WebPage (1)
- Session State in Asp.Net OverView (1)
- SharePoint 2010 – The form cannot be rendered. This may be due to a misconfiguration of the Microsoft SharePoint Server State Service. For more information (1)
- Tooltip GridView Header (1)
- Tree View Menu Control (1)
- WCF Service (12)
- What is Encapsulation in OOPS? (1)
0 Responses to “Gridview Inside a GridView”
Post a Comment