How to dynamically add gridview in asp.net c#

前端 未结 3 1675
无人及你
无人及你 2021-01-28 06:01

Hello guys i have to dynamically add multiple gridview in asp.net. There are no of gridview are genereated on the basis of selection.

相关标签:
3条回答
  • 2021-01-28 06:17

    I agree completely with @RMadd. SO is not a code-writing service, you should first try and give us something to look at and show you where you are going wrong.

    But, if you have no idea where to start, one way is to add all of the empty gridviews with no visibility and set them as visible according to the selection.

    That's about as vague of an answer as I can give for your vague question.

    Here are some other places to look I found with Google-foo:

    http://www.dotnetfox.com/articles/create-dynamic-gridview-or-programmatically-create-Asp-Net-gridview-with-dynamic-boundfield-1083.aspx

    How to dynamically add gridviews side by side using asp.net c#

    http://codedisplay.com/runtime-dynamically-creating-gridview-and-bind-data-in-bound-columns-using-asp-net-c-vb-net/

    0 讨论(0)
  • 2021-01-28 06:33
    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();
        }
    
    0 讨论(0)
  • 2021-01-28 06:34

    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.

    0 讨论(0)
提交回复
热议问题