Thursday, October 23, 2014

Grid view Dynamically

2 Method to Create Grid view Dynamically
Postback will disappear , Every postback need to create again.

a. Generate GridView

public
GridView GenerateGridView(int m_instanceCount)
{
    GridView newGridView = new GridView();
    newGridView.ID = GvBroadcastTitle.ID + m_instanceCount;
    newGridView.CssClass = "tabsummary";
    newGridView.GridLines = GridLines.None;
    newGridView.AlternatingRowStyle.CssClass = "alt";
    newGridView.AutoGenerateColumns = false;
    newGridView.AllowPaging = false;
    newGridView.CellSpacing = 1;
    newGridView.PageIndexChanging += new GridViewPageEventHandler(GvTitle_PageIndexChanging);
    newGridView.RowCommand += new GridViewCommandEventHandler(GvTitle _RowCommand);

    //Title ItemTemplate
    templateField titleField = new TemplateField();
    titleField.HeaderText = "Title";
    titleField.ItemTemplate = new AddTitleTemplateToGridView();
    newGridView.Columns.Add(titleField);

    // Bound Field
    foreach (DataControlField dc in GvTitle.Columns)
    {
      if (dc.GetType() == typeof(BoundField))
      {
        BoundField col = new BoundField();
        col.DataField = ((BoundField)dc).DataField;
        col.HeaderText = dc.HeaderText;
        newGridView.Columns.Add(col);
       }
     }

       return newGridView;
}

public class AddTitleTemplateToGridView : ITemplate
{
     LinkButton ht = new LinkButton();
     public AddTitleTemplateToGridView()}

     public virtual void OnInit(object sender, EventArgs e){ }
           
           
     void ITemplate.InstantiateIn(System.Web.UI.Control container)
     {
        ht = new LinkButton();
        ht.CommandName = "viewTitle";
        ht.Init += new EventHandler(OnInit);
        ht.DataBinding += new EventHandler(ht_DataBinding);
        container.Controls.Add(ht);
     }

     void ht_DataBinding(object sender, EventArgs e)
     {
        LinkButton lnk = sender as LinkButton;
        GridViewRow container = lnk.NamingContainer as GridViewRow;
        object dataValue = DataBinder.Eval(container.DataItem, "Title");
        if (dataValue != null)
        {
            lnk.Text = dataValue.ToString();
            lnk.CommandArgument = dataValue.ToString();
         }
       }
}


======================================================================================

b. Generate GridView

public GridView CopyGridView(GridView gridview, int m_instanceCount)
{
     Type t = gridview.GetType();
     GridView newGridView = (GridView)t.InvokeMember("", BindingFlags.CreateInstance, null, null, null);

     foreach (PropertyInfo prop in t.GetProperties())
     {
        if (prop.CanWrite)
        {
           if (prop.Name == "ID")
                 newGridView.ID = gridview.ID + m_instanceCount;
           else
                 prop.SetValue(newGridView, prop.GetValue(gridview, null), null);
        }
     }

     foreach (DataControlField item in gridview.Columns)
     {
          newGridView.Columns.Add(item);
     }
   
    newGridView.AlternatingRowStyle.CssClass = "alt";
    newGridView.RowCommand += new GridViewCommandEventHandler(GvTitle_RowCommand);               
     return newGridView;
}




No comments:

Post a Comment