How to dynamically add gridview in asp.net c#

前端 未结 3 1676
无人及你
无人及你 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: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();
        }
    

提交回复
热议问题