简体   繁体   中英

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"

so Is there any way? annotation or vs..

Set you Gridview's Auto "AutoGenerateColumns" property true. And then assign data-source with columns names.

And as you are using Devexpress, you must set data-source in 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. you can do this,

grid.Columns[0].Caption = "Customer Name"; grid.Columns[1].Caption = "Customer ID";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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