0

I can't seem to find an answer to this question: so is there a way to change the displayed column names in the GridView using property attributes in the data bound object?

I am binding data to the GridView through an ObjectDataSource, and in the specified object I want to be able to specify the display names for each column. Something like this:

[DisplayName("Datums")]
public DateTime Date { get; set; }

Is there a way to do so? Or do I necessary have to specify it through code behind of the display page?

4 Answers 4

2

There is no default attribute that will automagically set your column display names. perhaps you could set the names in your query? i.e. select name as "User name" from users

Sign up to request clarification or add additional context in comments.

2 Comments

Saddened. Well, I need to use an object datasource here. But thanks, best reply so far.
Maybe as a workaround you could make a 'translator' object? Yah, ugly, i know. but then you have 1 object where you 'translate' all column names. Then you can do a loop after setting the datasource
0

After getting your GridView's DataTable dtTable

do this with it

public DataTable Method()
{
foreach (DataRow dtRow in dtTable.Rows)
{
 // On all tables' columns
 foreach(DataColumn dc in dtTable.Columns)
 {
 if(dtRow[dc].ToString()=="SomeValue")
  {
   dtRow[dc].ToString()="SetYourValue";
  }
 }
}
return dtTable;
}

Use this dtTable as DataSourse .

Hope it works

1 Comment

Again, not really what I asked about. And the solution is kinda wonky.
0
List<whatever> src = new List<whatever>();
GridView1.DataSource = EnumerableToTable(src);

__

public static DataTable EnumerableToTable<T>(IList<T> li, PropertyType pt = PropertyType.Named)
    {
        DataTable table = EmptyTable<T>(pt);
        PopulateTableFromList<T>(table, ref li);
        return table;
    }
    public static void PopulateTableFromList<T>(DataTable table, ref IList<T> li, PropertyType pt = PropertyType.Named)
    {
        foreach (T obj in li)
        {
            List<PropertyInfo> propList = typeof(T).GetProperties().ToList();
            DataRow dr = table.NewRow();
            foreach (PropertyInfo prop in propList)
            {
                if (Attribute.IsDefined(prop, typeof(DisplayNameAttribute)))
                {
                    dr.SetField(prop.GetCustomAttributes(typeof(DisplayNameAttribute), false).Cast<DisplayNameAttribute>().Single().DisplayName, prop.GetValue(obj));
                }
                //else
                //{
                //    dr.SetField(prop.Name, prop.GetValue(obj));
                //}
            }
            table.Rows.Add(dr);
        }
    }
    public static DataTable EmptyTable<T>(PropertyType pt = PropertyType.Named)
    {
        //Create DataTable from list object's properties
        DataTable table = new DataTable();
        foreach (PropertyInfo prop in typeof(T).GetProperties())
        {
            if (pt != PropertyType.Browsable || !(Attribute.IsDefined(prop, typeof(BrowsableAttribute)) && !prop.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>().Single().Browsable))
            {
                if (Attribute.IsDefined(prop, typeof(DisplayNameAttribute)))
                {
                    if (!prop.PropertyType.Name.ToLower().Contains("null"))
                    {
                        table.Columns.Add(prop.GetCustomAttributes(typeof(DisplayNameAttribute), false).Cast<DisplayNameAttribute>().Single().DisplayName, prop.PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(prop.GetCustomAttributes(typeof(DisplayNameAttribute), false).Cast<DisplayNameAttribute>().Single().DisplayName, prop.PropertyType.GenericTypeArguments[0]);
                    }

                }
                else if (pt != PropertyType.Named)
                {
                    if (!prop.PropertyType.Name.ToLower().Contains("null"))
                    {
                        table.Columns.Add(prop.Name, prop.PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(prop.Name, prop.PropertyType.GenericTypeArguments[0]);
                    }

                }
            }
        }
        return table;
    }

Comments

0

Currently working in .NET Framework 4

Create your class to the hold the data you want to bind to.

namespace WebOrders
{
    public class OrderListItem
    {
        [DisplayName(@"WebOrderID")]
        public string WebOrderId { get; set; }
        [DisplayName(@"ID")]
        public string Id { get; set; }
        public string Location { get; set; }        

        public OrderListItem()
        {
        }

        public OrderListItem(string weborderId, string id, string location)
        {
            WebOrderId = weborderId;
            Location = location;
            Id = id;            
        }
    }
}

Then create a BindingSource from the tools menu.

BindingSource.DataSource should be the class name including the namespace. In this case the DataSource for my project is WebOrders.OrderListItem

The DataSource for the DataGridView will then be the name of the BindingSource. So in this case it will be bindingSourceOrderList

As soon as you bind to the bindingsource the columns should be created and the display name will be used as the column header text if the displayname is not filled in then the name is used.

To add data just use:

bindingSourceOrderList.Add(new OrderListItem(order.ID, obj.BatchId, obj.Location));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.