How can I get the DataGridView.Rows.DividerHeight to work?

I tried to make a simple sudoku interface using dataGridView. The problem is I can't get it to DividerHeight

work. The code below can 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)
    {

    } 
}

      

Are there any properties I should change to make it work?

I found a link suggesting dividerHeight not implemented:

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

It's true?

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

+3


source to share


2 answers


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

enter image description here

This is basically your code applied to a normal DataGridView recently wrapped in a 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 for the function getTable

.

Update: I was able to reproduce your problem. Mine DataGridView

is on TabPage

, which was not selected at startup. Calling the button code before , selecting the tab, I saw your error; when i entered the line to select the tab first it worked again .. Looks like a problem / error , DGV has its own layout when it is not visible ..

Note. It never works when you call it in the constructor! This is too early. Form_Load

great though ..

As a temporary workaround , you can move the code to the beginning Button

. (Having a start button is probably a good idea for the next round ..)

+3


source


DISCLAIMER . As TaW discovered, the OP's code works great when going from form constructor to Form_Load

. See his answer for details . I am leaving this answer solely for future reference and as a secondary option.


I believe it has something to do with binding DataSource

. For emphasis: I took your code (which also didn't work on the line separator for me) and decided to check out something else - because of your comment:

dividerHeight proposal not implemented

I've tested the following, making sure it's implemented:

dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.Red;
dataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Red;

      



The column was colored successfully, but the row remained unchanged. This led me to doubt if it was required as I saw binding causing some weird problems before styling, accessibility, etc. Indeed, binding DataSource

to other things (like List

custom string objects) also failed.

The above style change changes to strings, and the anchor "fails" because Form

and then DataGridView

is not visible yet. By default, visibility and bindings are not changed / processed behind the scenes until the constructor runs the course.


So I took your general idea, TaW's idea of โ€‹โ€‹fixing the width [and heights], and removed it DataSource

- replacing it with manual padding DataGridView

. The following output worked to accomplish the described desired result:

private List<int> dividers = new List<int>() { 2, 5 };
private int pad = 5;

public Form1()
{
    InitializeComponent();
    InitializeDataGridView();
}

private void InitializeDataGridView()
{
    dataGridView1.AllowUserToAddRows = false;

    for (int i = 0; i < 9; i++)
    {
        DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
        column.HeaderText = "c" + (i + 1);
        column.Width = 25 + (dividers.Contains(i) ? pad : 0);
        dataGridView1.Columns.Add(column);
    }

    for (int i = 0; i < 9; i++)
    {
        DataGridViewRow row = new DataGridViewRow();
        row.CreateCells(dataGridView1);
        row.Height = row.Height + (dividers.Contains(i) ? pad : 0);
        dataGridView1.Rows.Add(row);

        foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
        {
            cell.Value = cell.ColumnIndex;
        }
    }

    foreach (int div in dividers)
    {
        dataGridView1.Columns[div].DividerWidth = pad;
        dataGridView1.Rows[div].DividerHeight = pad;
    }
}

      

0


source







All Articles