我有一个绑定到datagridview的列表。我希望第一列是固定大小。数据绑定到dataGridView,我似乎找不到找到访问单个列属性的方法。如果尝试使用myDatagridview.colums [0],我会得到一个超出范围的索引,因为它说列数为0。

   private DataGridView setUpDataGrid(List<NVRlineVal> _NVRData)
    {
        //setup dataGridView
        DataGridView NVRDataGridView = new System.Windows.Forms.DataGridView();
        NVRDataGridView.ColumnHeadersHeightSizeMode =
            System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        NVRDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
        NVRDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


        NVRDataGridView.Name = "NVRDataGridView" + nvrIndex;
        NVRDataGridView.RowHeadersWidthSizeMode =
            System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
        NVRDataGridView.TabIndex = 0;
        NVRDataGridView.DataSource = _NVRData;
        //var clmn = NVRDataGridView.Columns[0];

        return NVRDataGridView;
    }


关于如何仅对其中一列具有固定列宽的任何想法,其余的将自动调整大小?

编辑原始代码,以显示仍可使用的更清理版本

最佳答案

我做类似的事情,但格式。

问题在于设置数据源后未绘制网格-您需要在DataBindingComplete事件中进行更改。

这是一些片段...

(来自https://minisqlquery.svn.codeplex.com/svn/trunk/MiniSqlQuery/QueryForm.cs

-网格清晰度:

        this.gridResults1.AllowUserToAddRows = false;
        this.gridResults1.AllowUserToDeleteRows = false;
        this.gridResults1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.ColumnHeader;
        this.gridResults1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.gridResults1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.gridResults1.Location = new System.Drawing.Point(3, 3);
        this.gridResults1.Name = "gridResults1";
        this.gridResults1.ReadOnly = true;
        this.gridResults1.Size = new System.Drawing.Size(1023, 242);
        this.gridResults1.TabIndex = 0;


-稍后订阅DataBindingComplete事件:

grid.DataBindingComplete += GridDataBindingComplete;


-处理事件(这里我正在修改格式,但是您尝试更改宽度):

    private void GridDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        DataGridView grid = sender as DataGridView;
        if (grid == null)
        {
            return;
        }
        DataTable dt = grid.DataSource as DataTable;
        if (dt == null)
        {
            return;
        }

        string nullText = _settings.NullText;
        string dateTimeFormat = _settings.DateTimeFormat;

        for (int i = 0; i < dt.Columns.Count; i++)
        {
            if (dt.Columns[i].DataType == typeof (DateTime))
            {
                DataGridViewCellStyle dateCellStyle = new DataGridViewCellStyle();
                dateCellStyle.NullValue = nullText;
                dateCellStyle.Format = dateTimeFormat;
                grid.Columns[i].DefaultCellStyle = dateCellStyle;
            }
        }
    }

关于c# - 修复列表绑定(bind)到datagridview时的列宽,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2465770/

10-14 11:21