简体   繁体   中英

Windows Forms DataGridView can't enter data in the last row

I was looking for a way to nicely enter data in the form of a matrix, and came across the DataGridView control. I create and edit it dynamically, and so far it worked quite well. However, now I am having this weird problem: I can edit all of the cells normally, but when I come to the last row and try to enter data, I see my values written over the cell values in the row above .

Here is my code example:

            DataGridView table = new DataGridView();
            table.RowCount = 3;
            table.ColumnCount = 3;
            table.ColumnHeadersVisible = false;
            table.RowHeadersVisible = false;
            table.ScrollBars = ScrollBars.None;
            for (int i = 0; i < 3;++i)
                table.Columns[i].Width = 40;
            int width = 40 * 3;
            int height = 22 * 3;
            table.Width = width;
            table.Height = height;
            this.Controls.Add(table);

( Note : the purpose of this code was just to see what I can do with the control, it isn't supposed to do anything useful.)

Also, I have seen someone on a forum saying that I should disable users from adding rows, but when I do that, I have only two rows (instead of three like I specified), and that gray area where the third row should be.

Edit : another weird thing I've noticed is that, if I click 3 times in the cell of the last row, I can edit it normally.

i Change your code, and it's work. do this:

 DataGridView table = new DataGridView();

        table.ColumnCount = 3;
        table.Rows.Add(3);
        table.ColumnHeadersVisible = false;
        table.RowHeadersVisible = false;
        table.ScrollBars = ScrollBars.None;
        for (int i = 0; i < 3; ++i)
            table.Columns[i].Width = 40;
        int width = 40 * 3;
        int height = 22 * 3;
        table.Width = width;
        table.Height = height;
        this.Controls.Add(table);
        table.AllowUserToAddRows = false;

first Add your columns and then add rows with table.Rows.Add(Number of rows), and at the end of code set allow users to add rows to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM