Search code examples
c#asp.netgridviewdatafield

how to create item template/templatefield for dynamic gridview


I have a code to create a GridView. It displays fine except for the SenderDetails column. I need to create an item template for this. The Message and Date DataFields come from a class called Chat and they display fine in the grid. But SenderDetails is called from another class in the chat class:

public class Chat : BaseResultSet
{
    public string Message { get; set; }
    public DateTime? SentDate { get; set; }
    public ChatUserDetails SenderDetails { get; set; }
}

The ChatUserDetails class is:

public class ChatUserDetails : BaseDisplaySet
{
    public string UserName { get; set; }
    public string CompanyName { get; set; }
    public bool Connected { get; set; }
}

So instead of displaying the username it displays baseClasses.Chat.ChatUserDetails in the SenderDetails column.

I need to display the UserName in the BuildChatsGrid(). GridView code:

public static GridView BuildChatsGrid()
{
    GridView NewDg = new GridView();

    NewDg.Columns.Add(new BoundField { DataField = "Message", HeaderText = "Note" });
    NewDg.Columns.Add(new BoundField { DataField = "SenderDetails", HeaderText = "Entered By" });  //need item template
    NewDg.Columns.Add(new BoundField { DataField = "SentDate", HeaderText = "Date", DataFormatString = "{0:dd/MM/yyyy}" });
}

So how do I add an item template or templatefield to call SenderDetails?


Solution

  • You need to override the ITemplate interface. like

    public class CreateItemTemplate : ITemplate
    {
        private ListItemType listItemType;
        private string _ColumnName;
    
        public CreateItemTemplate() { }
    
        public CreateItemTemplate(ListItemType Item, string ColumnName)
        { 
            listItemType = Item;
            _ColumnName = ColumnName;
        }
    
        public void InstantiateIn(System.Web.UI.Control container)
        {
            if (listItemType == ListItemType.Item)
            {
    
                Label lblUserData = new Label();
                lblUserData.DataBinding += new EventHandler(DataFormatter);
                container.Controls.Add(lblUserData);
            }
        }
        void DataFormatter(object sender, EventArgs e)
        {
            //Here you can write logic to display data
            Label lbl = (Label)sender;
    //(Below line)Here we are getting the container, that is GridViewRow which we are binding with our item template. Since there is a data source for this gridview (you surely assigned datasource), so each row will contain 'SenderDetails' object there.
                GridViewRow container = (GridViewRow)lbl.NamingContainer; 
    //Now we are extracting particular column data from GridViewRow object, we also know its type, that is ChatUserDetails 
            var objChatUserDetails = (ChatUserDetails )DataBinder.Eval(container.DataItem, _ColumnName);
            if (objChatUserDetails != null)
            {
                lbl.Text = "UserName : " + objChatUserDetails.UserName + ", CompanyName : " + objChatUserDetails.CompanyName ;
            }
        }
    }
    

    Now modify your BuildChatsGrid() function as

    public static GridView BuildChatsGrid()
    {
        GridView NewDg = new GridView();
    
        NewDg.Columns.Add(new BoundField { DataField = "Message", HeaderText = "Note" });
        //NewDg.Columns.Add(new BoundField { DataField = "SenderDetails", HeaderText = "Entered By" });  //need item template
        NewDg.Columns.Add(GetTemplateField("SenderDetails")); //Newly addded
        NewDg.Columns.Add(new BoundField { DataField = "SentDate", HeaderText = "Date", DataFormatString = "{0:dd/MM/yyyy}" });
    }
    

    Now create the above function GetTemplateField() in the current class like

    public TemplateField GetTemplateField(string colName)
    {
        TemplateField tfObject = new TemplateField();
        tfObject.HeaderText = "Entered Byte";
        tfObject.ItemTemplate = new CreateItemTemplate(ListItemType.Item, colName);
        return tfObject;
    }
    

    This function is calling the object of CreateItemTemplate class defined above. You can write data display logic above in DataFormatter() function.