question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataGridView unshare rows in VirtualMode despite all conditions to prevent it are met.

See original GitHub issue

.NET version

6.0.201

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Tested with .NET Framework 4.6.1, same issue.

Issue description

When creating DataGridView and setting VirtualMode to true it is not possible to prevent rows from becoming unshared.

As far as i see, i did everything that is described in this article to prevent rows becoming unshared, but no luck. This article is not updated for .NET Core or later.

Please take a look at the video, rows becoming unshared on row (cell) select (in 4.6.1 same behavior) but in 6.0.2 it also for some reason incrementally unshare all rows from 0 to the end in inconsistent manner (sometimes it won’t start it unless row changed with arrow keys or some other woodoo)

https://user-images.githubusercontent.com/102589197/160618007-a6077a18-8869-4388-9cbd-52ea604f4fec.mp4

Steps to reproduce

Code below is not exactly what i’m trying to achieve, it is a result of experiments to see if i can solve this issue. My goal is to display large set of data using virtual mode without unsharing rows.

namespace WinFormsApp1;

internal static class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new Form1());
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        grid.ReadOnly = true;
        grid.AllowUserToResizeColumns = false;
        grid.AllowUserToResizeRows = false;
        grid.ShowCellToolTips = false;
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "One", Name = "c_one1" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Two", Name = "c_one2" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Three", Name = "c_one3" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Four", Name = "c_one4" });
        grid.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()) { HeaderText = "Five", Name = "c_one5" });
        grid.VirtualMode = true;
        grid.EnableHeadersVisualStyles = false;
        grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        grid.RowHeadersVisible = false;
        grid.MultiSelect = false;
        grid.ColumnHeadersVisible = false;
        grid.RowUnshared += Grid_RowUnshared;
        grid.CellValueNeeded += Grid_CellValueNeeded;
        grid.RowCount = 1000000;
    }

    private void Grid_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
    {
        e.Value = e.RowIndex.ToString();
    }

    private void Grid_RowUnshared(object sender, DataGridViewRowEventArgs e)
    {
        tb.AppendText($"Row {e.Row.Index} unshared" + Environment.NewLine);
    }

    #region Designer
    private System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
            components.Dispose();
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.grid = new System.Windows.Forms.DataGridView();
        ((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
        this.SuspendLayout();
        // 
        // grid
        // 
        this.grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.grid.Location = new System.Drawing.Point(22, 12);
        this.grid.Name = "grid";
        this.grid.RowTemplate.Height = 25;
        this.grid.Size = new System.Drawing.Size(534, 412);
        this.grid.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.grid);
        this.Name = "Form1";
        this.Text = "Form1";
        //textbox
        tb = new TextBox();
        tb.Size = new Size(200, 412);
        tb.Location = new Point(22 + 536, 12);
        tb.Multiline = true;
        Controls.Add(tb);

        ((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
        this.ResumeLayout(false);
    }

    private DataGridView grid;
    private TextBox tb;
    #endregion    
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kirsan31commented, Mar 30, 2022

I can to look into it after finish with https://github.com/dotnet/winforms/issues/6859. But sadly have very few free time lately 😦

0reactions
msftbot[bot]commented, May 5, 2022

This issue is now marked as “up for grabs”, and we’re looking for a community volunteer to work on this issue. If we receive no interest in 120 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

Read more comments on GitHub >

github_iconTop Results From Across the Web

DataGridView not dispose old elements · Issue #6859
kirsan31 mentioned this issue on Mar 29, 2022. DataGridView unshare rows in VirtualMode despite all conditions to prevent it are met. #6930.
Read more >
c# - datagridview and row unsharing
I've encountered a performance issue with the C# DataGridView ... the form seems to cause all of the displayed rows to become unshared....
Read more >
Best Practices for Scaling DataGridView Control
Using shared rows. Preventing rows from becoming unshared. If you have special performance needs, you can implement virtual mode and provide ...
Read more >
Prevent Row Addition and Deletion in DataGridView Control
Learn how to prevent row addition and deletion in the Windows Forms DataGridView control via code examples in C# and Visual Basic.
Read more >
DataGridView Control - dotnetspider.com
Multiple options for changing cell, row, column, and header appearance and behavior. The DataGridView control enables you to work with individual grid.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found