Adding Serial# column programatically using DbContext


Adding Serial# column programatically using DbContext



How can a serial# (auto-incremented integer) column be added on-the-fly, at the 0th index, to the Data/List returned by a data-fetching command such as follows:


var averages = dbContextObj.MinuteAverages
.Where(x => x.ID == selectedID &&
x.Timestamp >= startDate &&
x.Timestamp <= endDate)
.OrderBy(x => x.Timestamp).ToList();



This column along with actual data from MinuteAverages has to be added to a DataGridView and should be bound to corresponding retrieved records (as I assume it would) for sorting purposes.


MinuteAverages


DataGridView





Better, simpler and working answer is here: stackoverflow.com/questions/25697917/…
– Pihu Mehra
Jul 9 at 4:45





1 Answer
1



First method



Create a ViewModel to extends the MinuteAverage properties
You can use Automapper to make your code easier


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



And then, convert the MinuteAverage collection to an AveragesVM collection with the Id filled.


//remeber to add using System.Linq;

var averagesWithId = averages.Select(new AveragesVM(x))
.ForEach(x=> x.Id= averages.Indexof(x));



Second method



Also you can listen to databound event


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





Thanks Badulake. I want to use your first solution as it seems more appropriate and efficient. But VS issues error on new AveragesVM(x) at x that it doesn't exist in current context. Also ForEach is not being recognized. Do I have to include some reference or what?
– Pihu Mehra
Jul 5 at 8:54


new AveragesVM(x)


x





Secondly I think (from GridView1_RowDataBound) that your solution might be valid for ASP.NET env while I have asked question for WinForms as indicated by the tag.
– Pihu Mehra
Jul 5 at 8:57


GridView1_RowDataBound





I detailed more the response @PihuMehra
– Badulake
Jul 5 at 9:30






Your updated code issues an error on var mapper= Mapper.Initialize... that "cannot assign void to an implicitly-typed variable" (because Initialize() returns void)
– Pihu Mehra
Jul 6 at 6:52



var mapper= Mapper.Initialize...


Initialize()


void





Also, the constructor cannot return any value while you are returning the Mapping.
– Pihu Mehra
Jul 6 at 10:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages