首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在DataGridView中更改列宽?

如何在DataGridView中更改列宽?
EN

Stack Overflow用户
提问于 2012-08-13 07:01:36
回答 3查看 116.6K关注 0票数 19

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

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

更新:

代码语言:javascript
复制
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;
    }
}
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-13 08:09:37

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

代码语言:javascript
复制
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

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

票数 30
EN

Stack Overflow用户

发布于 2018-08-14 13:05:46

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

代码语言:javascript
复制
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
票数 5
EN

Stack Overflow用户

发布于 2020-05-04 15:19:24

在我的Visual Studio2019中,只有在我将AutoSizeColumnsMode属性设置为None之后,它才能工作。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11926534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档