Display multiple qualification for each Employee in nested GridView with RowDataBound event using C# in ASP.Net

Last Reply on Oct 06, 2016 06:04 AM By AnandM

Posted on Sep 27, 2016 01:57 AM

Hiii,

I want to split cell or merge of the gridview row based on database records.

Same like below :

please help me..

Thanx in advance 

Sr.No Employee Name Designation Group  Qualification Subject Work Exp Area Specialisation Photo
1 John Tom Accountant Accounts M.Com Fin-1 Cash ABC
B.Com Fin-2 Online BCD
PhD P-1    
2 P K Johns Technician Technical ITI Sub-1     Photo
B.Tech Sub-2
3  Kevin Ob Adminstrator Admin BBA Marketing Exp-1 Business Photo
MBA Finance Exp-2 Cash
PGDMA P-1 Exp-3 Administration
Exp-4 Speci
You are viewing reply posted by: AnandM on Oct 06, 2016 06:04 AM.
Posted on Oct 06, 2016 06:04 AM

Hi Waghmare,

I have created a sample which full fill your requirement

HTML

<div align="center">
    <asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false" DataKeyNames="EmpId"
        OnRowDataBound="OnRowDataBound">
        <Columns>
            <asp:BoundField DataField="EmpId" HeaderText="Sr.No" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Designation" HeaderText="Designation" />
            <asp:BoundField DataField="GroupName" HeaderText="GroupName" />
            <asp:TemplateField HeaderText="Qualification">
                <ItemTemplate>
                    <asp:GridView ID="gvQualification" ShowHeader="false" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:BoundField DataField="QualifictionDegree" />
                        </Columns>
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Subject">
                <ItemTemplate>
                    <asp:GridView ID="gvSubject" ShowHeader="false" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:BoundField DataField="SubjectName" />
                        </Columns>
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gvEmployees.DataSource = GetData("SELECT EmpId,Name,Designation,GroupName FROM Employee");
        gvEmployees.DataBind();
    }
}

private static DataTable GetData(string query)
{
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = query;
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string EmpId = gvEmployees.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView gvQualification = e.Row.FindControl("gvQualification") as GridView;
        GridView gvSubject = e.Row.FindControl("gvSubject") as GridView;
        gvQualification.DataSource = GetData(string.Format(@"SELECT Qualifiction.QualifictionDegree
	                                                                  
                                                                FROM [QualEmpLinkTable] 
                                                                INNER JOIN Employee
                                                                ON [QualEmpLinkTable].EmpId = Employee.EmpId
                                                                INNER JOIN Qualifiction
                                                                ON Qualifiction.QualId = [QualEmpLinkTable].QualID
                                                                WHERE Employee.EmpId ='{0}'", EmpId));
        gvQualification.DataBind();
        gvSubject.DataSource = GetData(string.Format(@"SELECT [Subject].SubjectName
                                                                FROM [QualEmpLinkTable] 
                                                                INNER JOIN Employee
                                                                ON [QualEmpLinkTable].EmpId = Employee.EmpId
                                                                INNER JOIN [Subject]
                                                                ON [Subject].SubjectId = [QualEmpLinkTable].SubjectID
                                                                WHERE Employee.EmpId ='{0}'", EmpId));
        gvSubject.DataBind();
    }
}

ScreenShot