how to define constant size for particular cell in datagridview in Visual Studion Winforms?

I want to keep a constant size for rows and columns in Winforms Application Visual Studio V2015. Below is the code for the datagridview I’m using.

Image of the datagrid:- https://drive.google.com/open?id=1BpoW0WygC9mrSrxy4xUhJh0fdQozm0t9

        table.Columns.Add("SR.NO", typeof(int));
        table.Columns.Add("NAME", typeof(int));
        table.Columns.Add("CLASS", typeof(int));
        table.Columns.Add("ROLL NO", typeof(int));
        table.Columns.Add("GR.NO", typeof(int));
        table.Columns.Add("ADHAAR CARD UID", typeof(int));
        table.Columns.Add("GENDER", typeof(int));
        table.Columns.Add("CONTACT", typeof(int));
        table.Columns.Add("ADDRESS", typeof(int));
        dataGridView1.DataSource = table;

Answer

I did a quick demo project, showing how to use RowTemplate property to disable row resizing (see MSDN).

public partial class frmDataGridView : Form
{
    public frmDataGridView()
    {
        InitializeComponent();
    }

    private void frmDataGridView_Load(object sender, EventArgs e)
    {
        // disable resizing of rows
        dataGridView1.RowTemplate.Resizable = DataGridViewTriState.False;

        dataGridView1.RowTemplate.Height = 75; // set to preferred (your personal) default

        // datasource - structure
        DataTable table = new DataTable();
        table.Columns.Add("SR.NO", typeof(int));
        table.Columns.Add("NAME", typeof(int));
        table.Columns.Add("CLASS", typeof(int));
        table.Columns.Add("ROLL NO", typeof(int));
        table.Columns.Add("GR.NO", typeof(int));
        table.Columns.Add("ADHAAR CARD UID", typeof(int));
        table.Columns.Add("GENDER", typeof(int));
        table.Columns.Add("CONTACT", typeof(int));
        table.Columns.Add("ADDRESS", typeof(int));

        // datasource - data
        table.Rows.Add(1, 1, 1, 1, 1, 1, 1, 1, 1);

        dataGridView1.DataSource = table;

        dataGridView1.Columns[0].Width = 40; // set a specific width for first column in DataGridView
    }
}

This would result in a form with a datagridview like this:

DataGridView fixed row height

You can set Width property for every column you want (see DataGridViewColumn.Width).

Leave a Reply

Your email address will not be published. Required fields are marked *