3

我试图在一列中组合多个字段。就像一个人的两个名字(名字和姓氏)。是否可以在 DevExpress MVC GridView 上进行而不更改DB query

这是我在部分视图中的代码 -

    settings.Columns.Add("CompanyName");
    // I need to combine NurseFirstName and NurseLastName into one column and name as NurseName 
    settings.Columns.Add("NurseFirstName"); 
    settings.Columns.Add("NurseLastName");

TIA。

4

2 回答 2

8

您将需要使用 DataItemTemplate 将值组合到网格上的一个单元格中。就像是:

settings.Columns.Add(c =>
{
    c.Caption = "Nurse Name";
    c.SetDataItemTemplateContent(t =>
    {
        Html.DevExpress().Label(
            l => {
                l.Text = String.Format("{0} {1}", DataBinder.Eval(t.DataItem, "NurseFirstName"), DataBinder.Eval(t.DataItem, "NurseLastName"));
            }).Render();
    });
});
于 2013-12-27T15:09:00.127 回答
3

这是一个不会与模板混淆的更强大的解决方案:

    // Add column
    settings.Columns.Add(column =>
    {
        column.FieldName = "FullName";
        column.Caption = "Full Name";
        column.UnboundType = DevExpress.Data.UnboundColumnType.String;
    });

    // Add the combined values to the column
    settings.CustomUnboundColumnData = (sender, e) =>
    {
        if (e.Column.FieldName == "FullName")
        {
            string fName = (string)e.GetListSourceFieldValue("FirstName");
            string lName = (string)e.GetListSourceFieldValue("LastName");
            e.Value = string.Format("{0} {1}", fName, lName);
        }
    };
于 2014-08-13T14:43:15.270 回答