【问题标题】:How to change column width in DataGridView?如何更改 DataGridView 中的列宽?
【发布时间】:2012-08-09 05:22:11
【问题描述】:

我使用 Visual Studio 的 SQL Server Compact 3.5 创建了一个数据库和表,并将数据集作为我的数据源。在我的 WinForm 上,我有一个包含 3 列的 DataGridView。但是,我一直无法弄清楚如何让列占据 DataGridView 的整个宽度,如下图所示。

我想让缩写列更宽,然后让描述列一直延伸到表单的边缘。有什么建议吗?

更新:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuickNote
{
public partial class hyperTextForm : Form
{
    private static hyperTextForm instance;

    public hyperTextForm()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Normal;
        this.MdiParent = Application.OpenForms.OfType<Form1>().First();            
    }

    public static hyperTextForm GetInstance()
    {
        if (instance == null || instance.IsDisposed)
        {
            instance = new hyperTextForm();
        }

        return instance;
    }

    private void abbreviationsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.abbreviationsBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.keywordDBDataSet);
    }

    private void hyperTextForm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'keywordDBDataSet.abbreviations' table. You can move, or remove it, as needed.
        this.abbreviationsTableAdapter.Fill(this.keywordDBDataSet.abbreviations);
        abbreviationsDataGridView.Columns[1].Width = 60;
        abbreviationsDataGridView.Columns[2].Width = abbreviationsDataGridView.Width - abbreviationsDataGridView.Columns[0].Width - abbreviationsDataGridView.Columns[1].Width - 72;
    }
}
}

【问题讨论】:

    标签: c# sql-server winforms sql-server-ce compact-database


    【解决方案1】:

    在我的 Visual Studio 2019 中,它仅在我将 AutoSizeColumnsMode 属性设置为 None 后才起作用。

    【讨论】:

      【解决方案2】:

      将“AutoSizeColumnsMode”属性设置为“Fill”。默认情况下,它设置为“NONE”。现在将在 DatagridView 中填充列。然后你可以相应地设置其他列的宽度。

      DataGridView1.Columns[0].Width=100;// The id column 
      DataGridView1.Columns[1].Width=200;// The abbrevation columln
      //Third Colulmns 'description' will automatically be resized to fill the remaining 
      //space
      

      【讨论】:

        【解决方案3】:

        您可以将 abbrev 列的宽度设置为固定像素宽度,然后将 description 列的宽度设置为 DataGridView 的宽度,减去其他列的宽度和一些额外边距的总和(如果您想要防止水平滚动条出现在 DataGridView 上):

        dataGridView1.Columns[1].Width = 108;  // or whatever width works well for abbrev
        dataGridView1.Columns[2].Width = 
            dataGridView1.Width 
            - dataGridView1.Columns[0].Width 
            - dataGridView1.Columns[1].Width 
            - 72;  // this is an extra "margin" number of pixels
        

        如果您希望描述列始终占据 DataGridView 宽度的“剩余部分”,您可以将类似上述代码的内容放入 DataGridView 的 Resize 事件处理程序中。

        【讨论】:

        • 我将它添加到我的表单的加载函数中,以便在表单加载后它会自动调整它的大小,但由于某种原因它根本不会调整它的大小。我什至尝试将它放入表单的构造函数中,但没有成功。
        • 嗯...您有任何隐藏的列吗?如果你愿意发布你的代码,我可以仔细看看。
        • 不,这些是我仅有的列。我看不出我的代码在哪里对您有用,因为我通过设计器创建了所有内容。我实际上以这种形式获得的唯一代码是您给我的代码。我尝试过的所有其他代码都已删除。
        • 您的 DataGridView 是否将 AutoSizeColumnsMode 属性设置为 None 以外的其他值?这将阻止您的代码设置列宽。
        • 好的,谢谢。我完全忘记了我之前做过的事情。我有最后一个问题。我目前在我的数据库中有 9 个项目,当我在 IDE 内部和外部运行程序并向数据库添加一个新项目时,id 永远不会像它应该的那样从 10 开始。相反,它从 -1 开始。知道为什么吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-10
        • 2017-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-21
        • 2023-03-07
        相关资源
        最近更新 更多