简体   繁体   English

GridControl中的显示名称

[英]Display Name in GridControl

I have got simple entities that are; 我有一些简单的实体。

public class Customer
{
[Key]
public int ID { get; set; }

public String Name { get; set; }
}

public class Provider
{
[Key]
public int ID { get; set; }

public String Name { get; set; }
}

I am using 我在用

 Devexpress LinqServerModeSource
 Devexpress Grid

I set datasource on run time. 我在运行时设置数据源。

Grid Column titles are "ID" and "Name" , but if i set datasource customer, i wanna see "Customer Id" and "Customer Name" or if i set datasource provider, i wanna see "Provider Id" and "Provider Name" 网格列标题为“ ID”“名称” ,但如果我设置了数据源客户,我想查看“客户ID”“客户名称”,或者如果我设置了数据源提供者,则希望查看“ Provider ID”“ Provider Name”

so Is there any way? 所以有什么办法吗? annotation or vs.. 注释或vs.

Set you Gridview's Auto "AutoGenerateColumns" property true. 将Gridview的Auto“ AutoGenerateColumns”属性设置为true。 And then assign data-source with columns names. 然后为数据源分配列名。

And as you are using Devexpress, you must set data-source in Page Init. 在使用Devexpress时,必须在Page Init中设置数据源。 This is not perfect/complete code but you can get idea from this code block. 这不是完美/完整的代码,但是您可以从此代码块中获得启发。

protected void Page_InIt(object sender, EventArgs e)
{
    BindGrid("Customers");
}

private void BindGrid(string strDataSourceType)
{
    var customers, providers = null;

    if (strDataSourceType == "Customers")
    {
        customers = (from x in Context.Customers
                     select new
                     {
                         CustomerId = x.ID,
                         CustomerName = x.Name
                     }).ToList();
    }
    else if (strDataSourceType == "Providers")
    {
        providers = (from x in Context.Providers
                     select new
                     {
                         ProviderId = x.ID,
                         ProviderName = x.Name
                     }).ToList();
    }

    grid.DataSource = customers / providers;
    grid.AutoGenerateColumns = true;
    grid.DataBind();
}

Now, as you just want to change column name then you can do this way (Set Caption property), 现在,由于您只想更改列名,则可以使用这种方式(设置标题属性),

foreach (DevExpress.Web.ASPxGridView.GridViewDataColumn column in grid.Columns)
                {
                    if (column.FieldName == "Name")
                        column.Caption = "Customer Name";

                    if (column.FieldName == "ID")
                        column.Caption = "Customer ID";
                }

of if you know column order then you can set caption without using foreach loop. 如果您知道列顺序,则可以在不使用foreach循环的情况下设置标题。 you can do this, 你可以这样做,

grid.Columns[0].Caption = "Customer Name"; grid.Columns [0] .Caption =“客户名称”; grid.Columns[1].Caption = "Customer ID"; grid.Columns [1] .Caption =“客户ID”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM