问题
I have a datagridview control that is populated by a linq query:
public static IQueryable SearchByDepartmentNameInfo(string deptName)
{
ExamineDataContext dc = new ExamineDataContext();
var queryResult = from q in dc.GetTable<Question>()
where q.Topic.Module.Department.DepartmentName.Equals(deptName)
join s in dc.Solutions
on q.QuestionID equals s.QuestionID
into qs // note grouping
select new
{
Module = q.Topic.ModuleTitle,
Topic = q.TopicName,
Question = q.QuestionText,
QuestionType = q.QuestionType,
};
return queryResult;
}
dataGridView1.DataSource = Repository.SearchByDepartmentNameInfo("Computer Science");
I want the datagridview to automatically resize the columns based on the data returned from the query.
Help appreciated greatly.
回答1:
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
回答2:
Try adding behind that
dataGridView.AutoResizeColumns();
来源:https://stackoverflow.com/questions/816555/c-sharp-autosize-datagridview-populated-by-linq-query