Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make DataGridView.Rows.DividerHeight work?

I have tried to make a simple sudoku Interface using dataGridView. The problem is that I can't get DividerHeight to work. The code below is able to change the width of the vertical divider, but not the horizontal divider:

   public partial class Form1 : Form
{
    private DataTable sudokuTable; 

    public Form1()
    {
        InitializeComponent();
        sudokuTable = getTable();
        dataGridView1.DataSource = sudokuTable;

        for (int i = 0; i < 9; i++){                        
            dataGridView1.Columns[i].Width = 25;
        }

        dataGridView1.Columns[2].DividerWidth = 5; //Working
        dataGridView1.Columns[5].DividerWidth = 5; //Working
        dataGridView1.Rows[2].DividerHeight = 5; //Not working
        dataGridView1.Rows[5].DividerHeight = 5; //Not working

    }


    private static DataTable getTable()
    {
        DataTable newDataTable = new DataTable();


        for (int i = 0; i < 9; i++)
        {
            newDataTable.Columns.Add("c" + i+1, typeof(int));                       
        }

        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);
        newDataTable.Rows.Add(1, 2, 3, 4, 5, 6, 7, 8, 9);

        return newDataTable;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    } 
}

Is there some Properties I have to change to make it work?

I have found a link suggesting dividerHeight is not implemented:

http://www.visualwebgui.com/Developers/Forums/tabid/364/forumid/68/threadid/68506/scope/posts/Default.aspx

Is that true?

(I am using Visual Studio Community 2013 and Net Framework 4.5)

like image 952
Snofe Avatar asked Feb 01 '26 23:02

Snofe


1 Answers

Your code works just fine. With a few extra touches it looks like this:

enter image description here

This is basically your code, applied to a normal DataGridView freshly dropped to the form..:

private void button1_Click_1(object sender, EventArgs e)
{
    dataGridView1.DataSource = null;
    dataGridView1.Rows.Clear();
    dataGridView1.Columns.Clear();

    sudokuTable = getTable();
    dataGridView1.DataSource = sudokuTable;

    for (int i = 0; i < 9; i++)
    {
        dataGridView1.Columns[i].Width = 25 + ((i+1)%3 == 0 ? 5:0);
    }

    dataGridView1.Columns[2].DividerWidth = 5;
    dataGridView1.Columns[5].DividerWidth = 5; 
    dataGridView1.Rows[2].DividerHeight = 5; 
    dataGridView1.Rows[5].DividerHeight = 5; 

    dataGridView1.Rows[2].Height += 5;
    dataGridView1.Rows[5].Height += 5; 

    dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

    dataGridView1.RowHeadersVisible = false;
    dataGridView1.ColumnHeadersVisible = false;
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1[0, 0].Selected = false;
}

No changes at all to the getTable function..

Update: I was able to reproduce your problem. My DataGridView is on a TabPage which was not selected on startup. By calling the button click code before selecting the Tab I saw your error; when I put in a line to select the Tab first it worked again.. Looks like a problem/bug the DGV has with its layout when it isn't visible ..

Note: It never works when you call it in the constructor! That is too soon. Form_Load is fine, though..

As a temporary workaround, you can move the code to a Start Button. (Having a Start button is probably a good idea anyway, for the next round..)

like image 104
TaW Avatar answered Feb 03 '26 13:02

TaW