如何为动态gridview

时间:2015-08-21 14:47:22

标签: c# asp.net gridview datafield

我有一个代码来创建GridView。它显示正常,但SenderDetails列除外。我需要为此创建一个项目模板。 MessageDate DataField来自名为Chat的类,它们在网格中显示正常。但是SenderDetails是从聊天类中的另一个类调用的:

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

ChatUserDetails类是:

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

因此,代替显示用户名,它会在baseClasses.Chat.ChatUserDetails列中显示SenderDetails

我需要在BuildChatsGrid()中显示UserName。 GridView代码:

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}" });
}

那么如何添加项目模板或模板字段来调用SenderDetails

1 个答案:

答案 0 :(得分:3)

您需要覆盖ITemplate界面。像

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 ;
        }
    }
}

现在将BuildChatsGrid()函数修改为

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}" });
}

现在在当前类中创建上面的函数GetTemplateField(),如

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

此函数调用上面定义的CreateItemTemplate类的对象。您可以在DataFormatter()函数中编写上面的数据显示逻辑。