How to use Gridview Inside Another Controls using Asp.Net C#

 Gridview Inside Another Controls


Gridview inside access another controls like  Calender,Imagebutton,Gridview,Linkbutton Using in Asp.Net C#.

                                  DEMO

          
                             Download

                       HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use Gridview Inside Another Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <h4>Girdview Inside Button,Gridview,Linkbutton,
            ImageButton,Calender
        </h4>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle ForeColor="#999966" />
            <Columns>
                <asp:TemplateField HeaderText="Employee Name">
                    <ItemTemplate>
                        <table><tr><td>
                            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
                            </td><td>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Empname") %>'></asp:Label>
                            </td></tr>
                            <tr><td>
                                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
                                </td><td>
                                    <asp:ImageButton ID="ImageButton1" runat="server" Height="50px" ImageUrl="~/Penguins.jpg" OnClick="ImageButton1_Click" Width="50px" />
                                </td></tr>
                            <tr><td>
                                <h4>Child Gridview</h4>
                                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Empname">
                                            <ItemTemplate>
                                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Empname") %>'></asp:Label>
                                                <br />
                                                <br />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                                </td><td>
                                    <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
                                </td></tr>
                            <tr><td></td><td></td></tr>
                        </table>
                        &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Id" HeaderText="Employee Id">
                <ControlStyle Font-Size="Larger" />
                <ItemStyle HorizontalAlign="Center" />
                </asp:BoundField>
            </Columns>
            <HeaderStyle ForeColor="#3399FF" />
            <RowStyle ForeColor="Gray" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 C# CODING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class GridviewEvents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con=new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString() );
        con.Open();
        SqlDataAdapter adp=new SqlDataAdapter ("select * from Employee",con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // Button inside of Griview
        Button btn = (Button)sender;
        GridViewRow gr = (GridViewRow)btn.NamingContainer;
        Label lbl = (Label)gr.FindControl("Label1");
        ScriptManager.RegisterStartupScript(this,this.GetType(),"Script","alert('"+lbl.Text+"')",true);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        // LinkButton inside of Griview
        LinkButton lbt = (LinkButton)sender;
        GridViewRow gr = (GridViewRow)lbt.NamingContainer;
        Label lbl = (Label)gr.FindControl("Label1");
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script1", "alert('" + lbl.Text + "')", true);
       
    }  
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // GridView Inside of Another Gridview
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView grChild = e.Row.FindControl("GridView2") as GridView;
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select * from Employee", con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            grChild.DataSource = ds;
            grChild.DataBind();       
       
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton lbt = (ImageButton)sender;
        GridViewRow gr = (GridViewRow)lbt.NamingContainer;
        Label lbl = (Label)gr.FindControl("Label1");
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script12", "alert('" + lbl.Text + "')", true);
       
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Calendar cal = (Calendar)sender;
        GridViewRow gr = (GridViewRow)cal.NamingContainer;
        Calendar cal1 = (Calendar)gr.FindControl("Calendar1");
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script12", "alert('" +"Selected Date is = " + cal1.SelectedDate.ToString("dd/MM/yyyy") + "')", true);
    }

}
                    


Add New Webform - Select Gridview from toolbox - add controls to inside of gridview Calender,Linkbutton,Image button 




Next - Double Click All Controls - Add the Conditions for access gridview values and Child gridview rows




Retrive values from gridview rows events 





Html Coding for Generated all Controls 



       


0 comments:

Post a Comment