Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C通过数据绑定对象属性更改GridView列名_C#_Asp.net_Gridview - Fatal编程技术网

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

C# C通过数据绑定对象属性更改GridView列名,c#,asp.net,gridview,C#,Asp.net,Gridview,我似乎找不到这个问题的答案:那么有没有办法使用数据绑定对象中的属性来更改GridView中显示的列名 我通过ObjectDataSource将数据绑定到GridView,并且在指定的对象中,我希望能够为每个列指定显示名称。大概是这样的: [DisplayName("Datums")] public DateTime Date { get; set; } 有没有办法做到这一点?或者,在获取GridView的DataTable dtTable之后,是否需要通过显示页面的代码隐藏来指定它? 用它来做

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

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

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

有没有办法做到这一点?或者,在获取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用作数据源


希望它能工作

没有默认属性可以自动设置列显示名称。也许您可以在查询中设置名称? i、 e.从用户中选择名称作为用户名

__


目前正在.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应该是包含命名空间的类名。在本例中,我的项目的数据源是WebOrders.OrderListItem

DataGridView的数据源将是BindingSource的名称。因此,在本例中,它将是bindingSourceOrderList

绑定到bindingsource后,应立即创建列,并将显示名称用作列标题文本。如果未填写显示名称,则使用该名称

要添加数据,只需使用:

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

再说一次,不是我问的那样。这个解决方案有点站不住脚。我需要在这里使用一个对象数据源。但谢谢,这是迄今为止最好的回答。也许作为一种解决方法,你可以制作一个“译者”对象?是啊,丑陋,我知道。但是你有一个对象,在其中你“翻译”了所有的列名。然后可以在设置数据源后执行循环
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;            
        }
    }
}
bindingSourceOrderList.Add(new OrderListItem(order.ID, obj.BatchId, obj.Location));