19

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;
    }
}
}
4

3 に答える 3

30

省略形の列の幅を固定ピクセル幅に設定してから、説明の列の幅を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の幅の「余り」を占めるようにしたい場合は、上記のコードのようなものをResizeDataGridViewのイベントハンドラーに配置できます。

于 2012-08-13T00:09:36.537 に答える