如何在C#中调整datagridview中的列大小

时间:2014-10-09 16:30:05

标签: c# datagridview

我正在使用以下方法从SQL

填充datagridview
public bool _MFillGridView(string pQuery, ref DataGridView _pDgv)
{
    using (DataTable dt = new DataTable())
    {
        using (SqlConnection con = new SqlConnection(_CObjectsofClasses._obj_CConnectionString._MGetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand(pQuery, con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dt);
                    _pDgv.DataSource = dt;
                    _pDgv.Cursor = System.Windows.Forms.Cursors.Default;
                }
            }
        }

        foreach (DataColumn DC in dt.Columns)
        {
            _pDgv.Columns[DC.ColumnName].SortMode = DataGridViewColumnSortMode.NotSortable;

            if (DC.DataType == typeof(DateTime))
            {
               _pDgv.Columns[DC.ColumnName].DefaultCellStyle = new DataGridViewCellStyle { Format = _pDateFormat };
               _pDgv.Columns[DC.ColumnName].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
            else if (DC.DataType == typeof(decimal))
            {
               _pDgv.Columns[DC.ColumnName].DefaultCellStyle = new DataGridViewCellStyle { Format = _CObjectsofClasses._obj_CNumbricFunction._MFormatNo("0") };
            }
        }
    }

    return true;
}

在这里,我正在使用这个;

_MFillGridView("My SQL query here", ref MydatagridviewToFill);

上面的代码工作正常,但我面临列宽问题。我想传递列索引和DataGridViewAutoSizeColumnMode以及列宽以调整像

这样的列
coloumn[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
coloumn[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
coloumn[2].Width = 200;

以及未提供的所有其他列应设置为AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;

请建议我更改代码。

2 个答案:

答案 0 :(得分:3)

根据我对上述问题的理解,我相信这就是你想要的?

private void ResizeColumns(DataGridView gridview, int column, int width, DataGridViewAutoSizeColumnMode mode)
{
    for (int i = 0; i < gridview.ColumnCount; i++)
    {
        if (i.Equals(column))
        {
            gridview.Columns[i].AutoSizeMode = mode;
            gridview.Columns[i].Width = width;
        }
        else
        {
            gridview.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
        }
    }
}

更新

好吧仍然不确定我明白你的意思,但我认为这可能会指出你正确的方向

public bool _MFillGridView(string pQuery, ref DataGridView _pDgv, int columnIndex, int width)
{
    using (var dt = new DataTable())
    {
        // ... Code to retrieve data from Database ...

        for (var i = 0; i < dt.Columns.Count; i++)
        {
            var column = dt.Columns[i];
            if (column.DataType == typeof (DateTime))
                _pDgv.Columns[column.ColumnName].DefaultCellStyle = new DataGridViewCellStyle {Format = _pDateFormat};
            else if (column.DataType == typeof (decimal))
                _pDgv.Columns[column.ColumnName].DefaultCellStyle = new DataGridViewCellStyle {Format = _CObjectsofClasses._obj_CNumbricFunction._MFormatNo("0")};

            // Do your DataGridView formatting here
            if (_pDgv.Columns[column.ColumnName].Index.Equals(columnIndex)) // Check your Column index on the control
            {
                _pDgv.Columns[column.ColumnName].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
            else
            {
                _pDgv.Columns[column.ColumnName].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                _pDgv.Columns[column.ColumnName].Width = width;
            }
        }
    }
    return true;
}

答案 1 :(得分:0)

我使用@Mo Patel解决了我的问题我只是将int columnIndex,int width更改为List columnIndex,List width