Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..
i am having my menu on my Master page
each menu is linked to same Child web form

XML
<div>
    <ul>
        <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A1" href="MasterDataList.aspx?Reportid=1">Manage Country</a></li><br />
        <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A2" href="MasterDataList.aspx?Reportid=2">Manage User Type</a></li><br />
        <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A3" href="MasterDataList.aspx?Reportid=3">Manage User</a></li><br />
        <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A4" href="MasterDataList.aspx?Reportid=4">User Authorization</a></li><br />
       <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A5" href="MasterDataList.aspx?Reportid=5">Product Maintenence</a></li><br />
        <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A6" href="MasterDataList.aspx?Reportid=6">Manage Product Group</a></li><br />
       <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A7" href="MasterDataList.aspx?Reportid=7">Manage Product Assortment</a></li><br />
        <li style="border-left: 5px solid gray;margin-left:5px;"><a id="A8" href="MasterDataList.aspx?Reportid=8">Manage Product Family</a></li><br />
    </ul>
</div>

hence i need to know how can i add header text dynamically according to different masters selected
Posted
v2
Comments
Which GridView? Please post the code. Question is not clear. Please explain again.
Priyanka Bhagwat 2-Sep-13 6:44am    
any asp.net gridview
Priyanka Bhagwat 2-Sep-13 6:45am    
I found the solution....
thanks!!
Nelek 2-Sep-13 6:46am    
Then please post it, so other people facing the same problem can use it as well
Well done. :)

1 solution

I found the solution.....:)

its on this link
http://www.c-sharpcorner.com/uploadfile/jayendra/how-to-create-dynamic-gridview-using-boundfield/[^]

Step1: Your .aspx page will look like:
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicGridView.aspx.cs"
Inherits="DynamicGridView" %><!DOCTYPE html PUBLIC "
//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm
1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>


Step 2 : Your .cs page looks like:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class DynamicGridView : System.Web.UI.Page
{
    DataHelper helper = new DataHelper();
    List<Employee> emplist = new List<Employee>();
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateGridView();
    }
    private void CreateGridView()
    {
        GridView gv = new GridView();
        gv.ID = "EmployeeGridView";
        gv.AutoGenerateColumns = false;
        gv.AllowPaging = true;
        gv.EnableViewState = true;
        gv.PageSize = 10; // Default page Size

        //Create EventHanfler for Paging.

        gv.PageIndexChanging += new GridViewPageEventHandler(this.gv_PageIndexChanging);
        helper = new DataHelper();
        emplist = helper.GetEmployeeData();
        DataTable dt = new DataTable();
        if (emplist != null)
        {
            if (emplist.Count > 0)
            {
                dt = ListToDataTable(emplist);
 //We convert List view to DataTable because we use the
DataTable Column Name as a GridView Column Header.
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            BoundField boundField = new BoundField();
                            boundField.DataField = dt.Columns[i].ColumnName.ToString();
                            boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
                            gv.Columns.Add(boundField);
                        }
                    }
                }
            }
        }
        PlaceHolder1.Controls.Add(gv);
        BindGridView(gv, dt);
    }
    private void BindGridView(GridView gv, DataTable dt)
    {
        gv.DataSource = dt;
        gv.DataBind();
    }
    void gv_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        GridView gv = (GridView)sender;
        gv.PageIndex = e.NewPageIndex;
        helper = new DataHelper();
        emplist = helper.GetEmployeeData();
        DataTable dt = new DataTable();
        if (emplist != null)
        {
            if (emplist.Count > 0)
            {
                dt = ListToDataTable(emplist);
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        BindGridView(gv, dt);
                    }
                }
            }
        }
    }

    //Convert List To DataTable.
    static DataTable ListToDataTable<T>(IEnumerable<T> list)
    {
        var dt = new DataTable();
        foreach (var info in typeof(T).GetProperties())
        {
            dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
        }
        foreach (var t in list)
        {
            var row = dt.NewRow();
            foreach (var info in typeof(T).GetProperties())
            {
                row[info.Name] = info.GetValue(t, null);
            }
            dt.Rows.Add(row);
        }
        return dt;
    }
}
 
Share this answer
 
v3
Comments
Herbisaurus 2-Sep-13 10:28am    
5+!
jaideepsinh 4-Sep-13 3:33am    
+ve 5 !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900