如何将序列号(自动递增的整数)列即时添加到第0个索引到数据获取命令返回的数据/列表中,如下所示:
var averages = dbContextObj.MinuteAverages
.Where(x => x.ID == selectedID &&
x.Timestamp >= startDate &&
x.Timestamp <= endDate)
.OrderBy(x => x.Timestamp).ToList();
此列以及来自MinuteAverages
的实际数据必须添加到DataGridView
,并应绑定到相应的检索记录(如我想的那样)以进行排序。
答案 0 :(得分:0)
第一种方法
创建一个 ViewModel 来扩展 MinuteAverage 属性 您可以使用Automapper来简化代码
//remembet to add nuget and => using AutoMapper;
public class AveragesVM:MinuteAverage //inherit from your entity class retrieved from dbContextObj.MinuteAverages
{
public int Id{get;set;}
public AveragesVM:MinuteAverage(MinuteAverage minuteAverage)
{
var mapper= Mapper.Initialize(cfg => cfg.CreateMap<MinuteAverage,AveragesVM>());
return mapper.Map<AveragesVM>(minuteAverage);
}
}
然后,将 MinuteAverage 集合转换为填充了ID的 AveragesVM 集合。
//remeber to add using System.Linq;
var averagesWithId = averages.Select(new AveragesVM(x))
.ForEach(x=> x.Id= averages.Indexof(x));
第二种方法
您还可以收听数据绑定事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = e.Row;
row.Attributes["id"]=grdNews.DataKeys[e.Row.RowIndex].Value.ToString();
}
}