StringBuilder sb = new StringBuilder();
for (int k = 0; k < grid.Rows[0].Cells.Count; k++)
{
//add separator
sb.Append(grid.HeaderRow.Cells[k].Text + ';');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < grid.Rows.Count; i++)
{
for (int k = 0; k < grid.Rows[0].Cells.Count; k++)
{
//add separator
sb.Append(grid.Rows[i].Cells[k].Text.Replace(" "," ") + ';');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
Marked as answer by micnie2020 on Nov 05, 2012 11:38 AM
Congratulations! Im glad to hear that you have solved this issue by yourself, and it is very appreciated to share your solution to us. It may helpful for others. Welcome to post your question on this forum in the future programming.
Regards
Please mark the replies as answers if they help or unmark if not.
Feedback to us
micnie2020
Member
306 Points
524 Posts
MVC Controller bind linq.tolist to Grid View
Nov 05, 2012 07:39 AM|LINK
Hi All,
I tried to bind var result =(linq statement).tolist() to Grid view, but when i try to "using watch & breakpoint) grid.Columns.Count, result show 0.
Please advise.
Thank you.
var grid = new GridView(); var result = (from product in _repository.GetHomeExportMasters(module, search, lentity, filterExpression, "AID", " asc") join g in _repository.GetEntityList() on product.EntityID equals g.EntityID join orderDetails in _repository.GetGenAssetDetList(Session["filterExpression"].ToString()) on product.AID equals orderDetails.AID select new { //row id ID = product.AID, Asset_No = product.AssetNo, Legal_Entity = product.tblEntity.Description, Type = product.tblTypeSetup.Description, Department = product.tblDepartment.Description, Budget_ID = product.BudgetID, Segment = product.tblSegment.Description, Invoice_No = product.invoiceno.ToString(), Remark = orderDetails.Notes.ToString() }).ToList(); grid.DataSource =result; grid.DataBind(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=" + module.Replace(" ", "_") + ".csv"); Response.Charset = ""; Response.ContentType = "application/text"; StringBuilder sb = new StringBuilder(); for (int k = 0; k < grid.Columns.Count; k++) {micnie2020
Member
306 Points
524 Posts
Re: MVC Controller bind linq.tolist to Grid View
Nov 05, 2012 11:37 AM|LINK
Answer:
StringBuilder sb = new StringBuilder(); for (int k = 0; k < grid.Rows[0].Cells.Count; k++) { //add separator sb.Append(grid.HeaderRow.Cells[k].Text + ';'); } //append new line sb.Append("\r\n"); for (int i = 0; i < grid.Rows.Count; i++) { for (int k = 0; k < grid.Rows[0].Cells.Count; k++) { //add separator sb.Append(grid.Rows[i].Cells[k].Text.Replace(" "," ") + ';'); } //append new line sb.Append("\r\n"); } Response.Output.Write(sb.ToString()); Response.Flush(); Response.End();Young Yang -...
All-Star
21343 Points
1818 Posts
Microsoft
Re: MVC Controller bind linq.tolist to Grid View
Nov 12, 2012 12:55 AM|LINK
Congratulations! Im glad to hear that you have solved this issue by yourself, and it is very appreciated to share your solution to us. It may helpful for others. Welcome to post your question on this forum in the future programming.
Regards
Feedback to us
Develop and promote your apps in Windows Store