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();
John
  • 11
  • 3

1 Answers1

1
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

Community
  • 1
  • 1
Dylan Vester
  • 2,686
  • 4
  • 29
  • 40
  • I tried this, and unfortunately when binding to an empty datasource, the gridview still comes up blank without any header. – John Jan 11 '10 at 03:29