views:

293

answers:

1

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();
A: 
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:

http://stackoverflow.com/questions/354369/gridview-show-headers-on-empty-data-source

Dylan Vester
I tried this, and unfortunately when binding to an empty datasource, the gridview still comes up blank without any header.
John
Please see my edits
Dylan Vester