0

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?

Carsten
  • 11,287
  • 7
  • 39
  • 62
user123456789
  • 1,914
  • 7
  • 44
  • 100
  • Why don't you build this in your web form code? Is it imperative that it's built dynamically? – TestWell Aug 21 '15 at 14:57
  • @TestWell yes it must be built dynamically – user123456789 Aug 21 '15 at 14:57
  • 1
    [Here's a link I found](http://www.codeproject.com/Articles/13462/How-to-create-template-columns-dynamically-in-a-gr) that looks to address the same issue. You would have to adjust the template to suite your needs, but it's a start. Looks like any templates have to derive from `ITemplate` – TestWell Aug 21 '15 at 15:03

1 Answers1

3

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.

Lali
  • 2,816
  • 4
  • 30
  • 47
  • thanks for the answer. But I don't understand how `DataFormatter`is getting the username from the `ChatUserDetails` class. Should `sDetail` be referencing something? – user123456789 Aug 24 '15 at 10:37
  • I modified the answer with comments. please check it now. sDetail is typing error, it is ChatUserDetails in actuall. – Lali Aug 24 '15 at 10:48
  • thanks for adding the comments. But I get the error Error 6 An object reference is required for the non-static field, method, or property 'GridViewUtils.GridViewHelper.GetTemplateField(string)' in the BuildChatsGrid() function – user123456789 Aug 24 '15 at 10:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87770/discussion-between-liaqat-ali-and-user123456789). – Lali Aug 24 '15 at 11:03