Harshal
Harshal

Reputation: 41

How to dynamically add gridview

I have to dynamically add multiple gridview in ASP.net. There are no of gridview are genereated on the basis of selection.

Upvotes: 0

Views: 29857

Answers (3)

Mo Arkam khan
Mo Arkam khan

Reputation: 11


<asp:Panel ID="Panel1" runat="server"> </asp:Panel>


         DataSet ds = new DataSet();
        ds = obj.GetMedicalGridWithAge(MphID, ProductCode);
        if(ds.Tables.Count > 0)
        {
            if (ds.Tables[1].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables.Count; i++)
                {

                    GridView gv = new GridView();
                    gv.ID = "gv" + (i + 1);
                    
                    gv.DataSource = ds.Tables[i];
                    gv.DataBind();
                    Panel1.Controls.Add(gv);
                    Label newLine = new Label(); newLine.Text = "<br/>";
                    Panel1.Controls.Add(newLine);

                }
            }
            else
            {
                GridView gv = new GridView();
                gv.ID = "gv" ;

                gv.DataSource = null;
                gv.DataBind();
                Panel1.Controls.Add(gv);
            }
           
        }

Upvotes: 1

Code
Code

Reputation: 749

private void BindDynaicGrd()
    {
        //instance of a datatable
        DataTable dt = new DataTable();
        //instance of a datarow
        DataRow drow;
        //creating two datacolums dc1 and dc2 
        DataColumn dc1 = new DataColumn("Code", typeof(string));
        DataColumn dc2 = new DataColumn("Name", typeof(string));
        //adding datacolumn to datatable
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);

        if (grd.Rows.Count > 0)
        {
            foreach (GridViewRow gvr in grdSites.Rows)
            {

                CheckBox chk_Single = (CheckBox)gvr.FindControl("chkSingle");
                if (chk_Single.Checked == true)
                {
                    Label lbl_Code = (Label)gvr.FindControl("lblCode");
                    Label lbl_Name = (Label)gvr.FindControl("lblName");
                    //instance of a datarow
                    drow = dt.NewRow();
                    //add rows to datatable
                    //add Column values
                    drow = dt.NewRow();
                    drow["Code"] = lbl_Code.Text;
                    drow["Name"] = lbl_Name.Text.ToString();
                    dt.Rows.Add(drow);
                }
            }
        }
        //set gridView Datasource as dataTable dt.
        gridcl.DataSource = dt;
        //Bind Datasource to gridview
        gridcl.DataBind();
    }

Upvotes: 2

Rahul - Systematix
Rahul - Systematix

Reputation: 77

If I have not understood wrong from title that add multiple grid view dynamically means want to add grid view from code behind at run time.

As GridView is a class in ASP.NET C# and we can create object of it and set its properties just like other class object like follows:

GridView objGV = new GridView();
objGV .AutoGenerateColumns = false;

and can add columns of different type like BoundField and TemplateField from code Like follows:

BoundField field = new BoundField();
field.HeaderText = "Column Header";
field.DataField = Value;
objGV .Columns.Add(field);

and finally can add this grid view object on .aspx under any container control like panel.

PanelId.Controls.Add(objGV );

For adding multiple grid instance just iterate above code in loop like:

for(int i=0;i<yourConditionCount;i++)
{
    GridView objGV = new GridView();
     objGV.ID="GV"+i;   // ID of each grid view must be unique

    // your code logic to set properties and events for grid view

   PanelId.Controls.Add(objGV );
} 

Hope I understood your requirement correctly and my explanation will be helpful for you.

Upvotes: 5

Related Questions