John
John

Reputation: 11

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

Upvotes: 1

Views: 1481

Answers (1)

Dylan Vester
Dylan Vester

Reputation: 2726

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

Upvotes: 1

Related Questions