columns not shown when binding to gridview using linq to sql with an empty datasource
Is there a way to display the columns of the data you are selecting from when binding to a datagrid with an empty datasource? Whenever I bind with an empty datasource, the grid won't even show.
var results = from t in db.vwTaskInfos where t.PriorityId == Convert.ToInt32(drdPriority.SelectedValue) select t;
开发者_如何学运维 gvTasks.DataSource = results;
gvTasks.AutoGenerateColumns = true;
gvTasks.DataBind();
var results = from t in db.vwTaskInfos where t.PriorityId == Convert.ToInt32(drdPriority.SelectedValue) select t;
gvTasks.DataSource = results;
gvTasks.AutoGenerateColumns = true;
gvTasks.DataBind();
Change to:
var results = from t in db.vwTaskInfos where t.PriorityId == Convert.ToInt32(drdPriority.SelectedValue) select t;
gvTasks.DataSource = results.ToList();
gvTasks.AutoGenerateColumns = true;
gvTasks.DataBind();
Notice I changed "gvTasks.DataSource = results;" to "gvTasks.DataSource = results.ToList();"
EDIT:
I see, your problem is not actually a linq to sql issue. It's a grid view issue. That being said, here is the solution you're looking for:
GridView - Show headers on empty data source
精彩评论