ASP.NET - How to add dynamically itemtemplate with Checbox in dynamic gridview in asp.net (c#)

Asked By Sivanesh Vanmeeganathan on 19-Jun-12 01:45 AM

How can i add dynamically add itemtemplate with in Dynamic CheckBox in dynamic gridview from SQLServer database?

Jitendra Faye replied to Sivanesh Vanmeeganathan on 19-Jun-12 01:55 AM
Simply use this code-

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        CheckBox chk = new CheckBox();
        e.Row.Cells[0].Controls.Add(chk);
      }

    }

Try this and let me know.

Sivanesh Vanmeeganathan replied to Jitendra Faye on 19-Jun-12 02:14 AM
  if (e.Row.RowType == DataControlRowType.DataRow)
    {
      CheckBox chk = new CheckBox();
      e.Row.Cells[0].Controls.Add(chk);
    }

Its work but it is affect only in first column, i need all cells in GridView
Jitendra Faye replied to Sivanesh Vanmeeganathan on 19-Jun-12 02:25 AM
If you want to add for other column also then use this code-

if (e.Row.RowType == DataControlRowType.DataRow)
    {
    CheckBox chk = new CheckBox();
    e.Row.Cells[0].Controls.Add(chk);
    e.Row.Cells[1].Controls.Add(chk);
     e.Row.Cells[2].Controls.Add(chk);
-------------
--------------
---
    }
Chintan Vaghela replied to Sivanesh Vanmeeganathan on 19-Jun-12 02:32 AM

Hi Frndz,

 

Functionality:  Dynamic added Checkbox inside Dynamicaly Gridview

 

To achieve this task,

 

Need to create constructor for Item Template

 

public class MyCustomTemplate : ITemplate

    {

      public void InstantiateIn(System.Web.UI.Control container)

      {

}

}

 

Create Dynamic GridView set AutoGenerateColumn = False

 

GridView gvDynamic = new GridView();

 

Cretae Template Field Dynamic

 

      TemplateField tf = new TemplateField();

      tf.HeaderText = "Check";

      tf.ItemTemplate = new MyCustomTemplate();

 

Added Template filed to GridView Column

 

Bind Gridview with Data Source

 

Added Gridview to One Panel Control for Display this GridView.

 

 

 

 

Logic   :

 

GridView Logic

 

GridView gvDynamic = new GridView();

      gvDynamic.Width = Unit.Pixel(700);

      gvDynamic.BorderWidth = Unit.Pixel(0);

 

      gvDynamic.AutoGenerateColumns = false;

      gvDynamic.ShowFooter = true;

 

      TemplateField tf = new TemplateField();

      tf.HeaderText = "Check";

      tf.ItemTemplate = new MyCustomTemplate();

 

      gvDynamic.Columns.Add(tf);

      gvDynamic.DataSource = dt;

      gvDynamic.DataBind();

      this.Panel1.Controls.Add(gvDynamic);

 

 

ItemTemplate Constructor Logic

 

    public class MyCustomTemplate : ITemplate

    {

      public void InstantiateIn(System.Web.UI.Control container)

      {

        CheckBox cb = new CheckBox();

        cb.ID = "checkbox";

        cb.Text = "active";

        container.Controls.Add(cb);

      }

    }

 

 

Hope this helpful!

Thanks

 

 

 

Sivanesh Vanmeeganathan replied to Chintan Vaghela on 19-Jun-12 03:48 AM
Mutliple checkbox have same id ,
If i check first or second one checkbox automatically checked true at last checkbox
(i think last one checkbox id set all checkbox)


Kindly give me any another solution.