C# AutoSize DatagridView Populated By Linq Query

浪尽此生 提交于 2019-12-11 08:18:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!