如何在绑定数据源后更改datagridview列宽?

时间:2012-11-07 12:20:45

标签: winforms c#-4.0 datagridview datasource

如何更改datagridviewwidth?我尝试了以下方法,但它不起作用。为什么不变?

  string[] strArrDoctor = new string[] {"ID", "Name", "Specialization", "Contact No", "Experience(Years)", "Delete" };
           //strin of array which contains Patient table columns names
            string[] strArrPatient = new string[] {"ID", "Name", "Join Date", "Ailment", "Doctor ID", "Delete" };
            if (rBtnDoctor.Checked)
            {
                for (int intIndex = 0; intIndex < dgvShowRecords.Columns.Count; intIndex++)
                {
                    //assigning Column names with our specified String of arrays
                    dgvShowRecords.Columns[intIndex].HeaderText = strArrDoctor[intIndex];
                }
            }
            else
            {
                for (int intIndex = 0; intIndex < dgvShowRecords.Columns.Count;      intIndex++)
                {
                    dgvShowRecords.Columns[intIndex].HeaderText=strArrPatient[intIndex];
                }
            }
            //dgvShowRecords is Datagridview Name
            dgvShowRecords.Columns["Name"].Resizable = DataGridViewTriState.True;
            dgvShowRecords.Columns["Name"].Width = 200;
            dgvShowRecords.Columns["ID"].Width = 20;

请任何人知道为什么它没有变化让我知道。

enter image description here

1 个答案:

答案 0 :(得分:4)

尝试:

dgvShowRecords.AutoResizeColumns();