C#通过数据绑定对象属性属性更改GridView列名

时间:2014-06-11 12:24:14

标签: c# asp.net gridview

我似乎无法找到这个问题的答案:那么有没有办法使用数据绑定对象中的属性属性更改GridView中显示的列名?

我通过ObjectDataSource将数据绑定到GridView,并且在指定的对象中我希望能够为每列指定显示名称。像这样:

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

有没有办法这样做?或者我是否必须通过显示页面后面的代码指定它?

4 个答案:

答案 0 :(得分:2)

没有默认属性可以自动设置列显示名称。也许您可以在查询中设置名称? 即select name as "User name" from users

答案 1 :(得分:0)

获取GridView的DataTable dtTable后

用它做到这一点

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

将此dtTable用作DataSourse。

希望它有效

答案 2 :(得分: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;
    }

答案 3 :(得分:0)

目前在.NET Framework 4中工作

创建您的类以保存要绑定的数据。

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

然后从工具菜单中创建一个BindingSource。

BindingSource.DataSource应该是包含命名空间的类名。在这种情况下,我的项目的DataSource是WebOrders.OrderListItem

DataGridView的DataSource将是BindingSource的名称。所以在这种情况下它将是bindingSourceOrderList

一旦绑定到bindingsource,就应该创建列,如果未填充displayname,则显示名称将用作列标题文本,然后使用名称。

要添加数据,请使用:

bindingSourceOrderList.Add(new OrderListItem(order.ID, obj.BatchId, obj.Location));
相关问题