I am seeing really bad performance on the DataGridView that is bound to a DataTable with a boolean field. The problem occurs when you loop through the DataTable and try to change the value of the boolean field. In my example below I create a hard coded DataTable and add 26 rows to it and then when you click on the button it loops through the rows and changes the value of the boolean field.
I ran the code through a profiler and the line that sets the boolean field (row["IsAllowed"] = boolswitch;) takes an average of 2 seconds to run causing a noticeable
delay in the form. Anyone know what would cause this
--To recreate, create a windows application project and modify the Form1.cs and Form1.Designer.cs files with below. Performance issue occurs on line 84 of Form1.cs
--Form1.cs--
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataGridViewSample
{
public partial class Form1 : Form
{
private DataTable tableMDSSection;
private bool boolswitch;
public Form1()
{
InitializeComponent();
tableMDSSection = new DataTable("Test");
tableMDSSection.Columns.Add("Id", typeof(Int16));
tableMDSSection.Columns.Add("IsAllowed", typeof(bool));
tableMDSSection.Columns.Add("Section", typeof(string));
tableMDSSection.Columns.Add("Description", typeof(string));
tableMDSSection.PrimaryKey = new DataColumn[] { tableMDSSection.Columns["Id"] };
//Add the hard coded rows
tableMDSSection.Rows.Add(1, false, "001", "Description 1");
tableMDSSection.Rows.Add(2, false, "002", "Description 2");
tableMDSSection.Rows.Add(3, false, "003", "Description 3");
tableMDSSection.Rows.Add(4, false, "004", "Description 4");
tableMDSSection.Rows.Add(5, false, "005", "Description 5");
tableMDSSection.Rows.Add(6, false, "006", "Description 6");
tableMDSSection.Rows.Add(7, false, "007", "Description 7");
tableMDSSection.Rows.Add(8, false, "008", "Description 8");
tableMDSSection.Rows.Add(9, false, "009", "Description 9");
tableMDSSection.Rows.Add(10, false, "010", "Description 10");
tableMDSSection.Rows.Add(11, false, "011", "Description 11");
tableMDSSection.Rows.Add(12, false, "012", "Description 12");
tableMDSSection.Rows.Add(13, false, "013", "Description 13");
tableMDSSection.Rows.Add(14, false, "014", "Description 14");
tableMDSSection.Rows.Add(15, false, "015", "Description 15");
tableMDSSection.Rows.Add(16, false, "016", "Description 16");
tableMDSSection.Rows.Add(17, false, "017", "Description 17");
tableMDSSection.Rows.Add(18, false, "018", "Description 18");
tableMDSSection.Rows.Add(19, false, "019", "Description 19");
tableMDSSection.Rows.Add(20, false, "020", "Description 20");
tableMDSSection.Rows.Add(21, false, "021", "Description 21");
tableMDSSection.Rows.Add(22, false, "022", "Description 22");
tableMDSSection.Rows.Add(23, false, "023", "Description 23");
tableMDSSection.Rows.Add(24, false, "024", "Description 24");
tableMDSSection.Rows.Add(25, false, "025", "Description 25");
tableMDSSection.Rows.Add(26, false, "026", "Description 26");
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = this.tableMDSSection;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.Visible = false;
}
dataGridView1.Columns["IsAllowed"].Visible = true;
dataGridView1.Columns["IsAllowed"].HeaderText = string.Empty;
dataGridView1.Columns["IsAllowed"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns["IsAllowed"].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns["Section"].Visible = true;
dataGridView1.Columns["Section"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.Columns["Section"].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns["Section"].ReadOnly = true;
}
private void button1_Click(object sender, EventArgs e)
{
boolswitch = !boolswitch;
dataGridView1.SuspendLayout();
foreach (DataRow row in tableMDSSection.Rows)
{
row.BeginEdit();
row["IsAllowed"] = boolswitch;
row.EndEdit();
}
tableMDSSection.AcceptChanges();
dataGridView1.ResumeLayout();
}
}
}
Form1.Designer.cs
namespace DataGridViewSample
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 58);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(482, 354);
this.dataGridView1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(124, 28);
this.button1.TabIndex = 1;
this.button1.Text = "Update";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(506, 440);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
}
}

DataGridView, Really Bad Performance w/ Boolean Field