20

I have created a database and table using Visual Studio's SQL Server Compact 3.5 with a dataset as my datasource. On my WinForm I have a DataGridView with 3 columns. However, I have been unable to figure out how to get the columns to take up the full width of the DataGridView which is shown in the image below.

I want to make the abbreviates column wider and then have the description column extend all the way over to the edge of the form. Any suggestions?

Update:

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;
    }
}
}
Pallas
  • 1,499
  • 5
  • 25
  • 57

3 Answers3

31

You could set the width of the abbrev column to a fixed pixel width, then set the width of the description column to the width of the DataGridView, minus the sum of the widths of the other columns and some extra margin (if you want to prevent a horizontal scrollbar from appearing on the 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

If you wanted the description column to always take up the "remainder" of the width of the DataGridView, you could put something like the above code in a Resize event handler of the DataGridView.

hmqcnoesy
  • 4,165
  • 3
  • 31
  • 47
  • I added it to the my form's load function so that it would automatically size it as soon as the form loads, but for some reason it doesn't resize it at all. I even tried putting it in the form's constructor with no luck. – Pallas Aug 13 '12 at 00:37
  • Hmmm... Do you have any hidden columns? If you're willing to post your code, I can take a closer look. – hmqcnoesy Aug 13 '12 at 00:40
  • 1
    No, those are the only columns I have. I don't see where my code would be useful to you because I created everything via the designer. The only code I've actually got in this form was the code that you gave me. All other code that I've tried I have already removed. – Pallas Aug 13 '12 at 01:22
  • 3
    Does your DataGridView have the `AutoSizeColumnsMode` property set to something other than `None`? That would prevent your code from setting the column widths. – hmqcnoesy Aug 13 '12 at 01:26
  • 1
    Yes, thank you. I completely forgot that I had done that earlier. I have one last question. I currently have 9 items in my database and when I run the program both inside and outside the IDE and add a new item to the database the id never starts at 10 like it should. Instead it starts at -1. Any idea why? – Pallas Aug 13 '12 at 01:41
  • Your database table's `id` column is an identity type? That should let the database handle the incrementing instead of relying on your application to assign values. You should probably open a new question. – hmqcnoesy Aug 13 '12 at 01:47
6

Set the "AutoSizeColumnsMode" property to "Fill".. By default it is set to 'NONE'. Now columns will be filled across the DatagridView. Then you can set the width of other columns accordingly.

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
MShahid777
  • 71
  • 1
  • 4
3

In my Visual Studio 2019 it worked only after I set the AutoSizeColumnsMode property to None.

ejuhjav
  • 2,660
  • 2
  • 21
  • 32