Discussion:
RowState remains Unchanged after edit DataGridView
(too old to reply)
Lior Ronen
2009-06-14 11:30:01 UTC
Permalink
Hi,

After clicking the button The number of rows selected shows 2 instead of 3
and the RowState of the first row is Unchanged.

Is this a bug ?

Following is the code to reproduce the problem.
Just copy and paste it into a new Form.
Thanks.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <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.btnInvertSelection = 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(42, 67);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(362, 203);
this.dataGridView1.TabIndex = 0;
//
// btnInvertSelection
//
this.btnInvertSelection.Location = new System.Drawing.Point(42,
23);
this.btnInvertSelection.Name = "btnInvertSelection";
this.btnInvertSelection.Size = new System.Drawing.Size(144, 38);
this.btnInvertSelection.TabIndex = 1;
this.btnInvertSelection.Text = "Select/Unselect All";
this.btnInvertSelection.UseVisualStyleBackColor = true;
this.btnInvertSelection.Click += new
System.EventHandler(this.btnInvertSelection_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(458, 337);
this.Controls.Add(this.btnInvertSelection);
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 btnInvertSelection;

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();

DataColumn dc = new DataColumn();
dc.DataType = typeof(System.Boolean);
dc.ColumnName = "IsSelected";
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = typeof(System.String);
dc.ColumnName = "Country";
dt.Columns.Add(dc);

DataRow dr = dt.NewRow();
dr["Country"] = "USA";
dr["IsSelected"] = false;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["IsSelected"] = false;
dr["Country"] = "Spain";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["IsSelected"] = false;
dr["Country"] = "Canada";
dt.Rows.Add(dr);

dt.AcceptChanges();

dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToOrderColumns = true;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.SelectionMode =
System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
this.dataGridView1.RowHeadersVisible = false;

DataGridViewColumn col = new DataGridViewCheckBoxColumn();
col.DataPropertyName = "IsSelected";
col.Name = "IsSelected";
col.HeaderText = "IsSelected";
col.ReadOnly = false;
dataGridView1.Columns.Add(col);


col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Country";
col.Name = "Country";
col.HeaderText = "Country";
col.ReadOnly = true;
dataGridView1.Columns.Add(col);

dataGridView1.DataSource = dt;
m_checked = false;
}

private bool m_checked = false;
private void btnInvertSelection_Click(object sender, EventArgs e)
{
dataGridView1.EndEdit();

m_checked = !m_checked;

foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows)
{
dataGridViewRow.Cells["IsSelected"].Value = m_checked;
}

dataGridView1.EndEdit();

DataTable dataTable = (DataTable)dataGridView1.DataSource;
DataView dataView = new DataView(dataTable);
dataView.RowFilter = "IsSelected=true";
MessageBox.Show("No of Selected Rows: " +
dataView.Count.ToString());
MessageBox.Show("RowState of First Row in DataTable: " +
dataTable.Rows[0].RowState.ToString());


}
}
}
Cor Ligthert[MVP]
2009-06-14 16:48:44 UTC
Permalink
Lior,

Yes I have the idea there is a bug in your program.

Are you want to set all changes to unchanged

dt.AcceptChanges();

Cor
Post by Lior Ronen
Hi,
After clicking the button The number of rows selected shows 2 instead of 3
and the RowState of the first row is Unchanged.
Is this a bug ?
Following is the code to reproduce the problem.
Just copy and paste it into a new Form.
Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <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.btnInvertSelection = 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(42, 67);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(362, 203);
this.dataGridView1.TabIndex = 0;
//
// btnInvertSelection
//
this.btnInvertSelection.Location = new System.Drawing.Point(42,
23);
this.btnInvertSelection.Name = "btnInvertSelection";
this.btnInvertSelection.Size = new System.Drawing.Size(144, 38);
this.btnInvertSelection.TabIndex = 1;
this.btnInvertSelection.Text = "Select/Unselect All";
this.btnInvertSelection.UseVisualStyleBackColor = true;
this.btnInvertSelection.Click += new
System.EventHandler(this.btnInvertSelection_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(458, 337);
this.Controls.Add(this.btnInvertSelection);
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 btnInvertSelection;
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
dc.DataType = typeof(System.Boolean);
dc.ColumnName = "IsSelected";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = typeof(System.String);
dc.ColumnName = "Country";
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["Country"] = "USA";
dr["IsSelected"] = false;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["IsSelected"] = false;
dr["Country"] = "Spain";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["IsSelected"] = false;
dr["Country"] = "Canada";
dt.Rows.Add(dr);
dt.AcceptChanges();
dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToOrderColumns = true;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.SelectionMode =
System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
this.dataGridView1.RowHeadersVisible = false;
DataGridViewColumn col = new DataGridViewCheckBoxColumn();
col.DataPropertyName = "IsSelected";
col.Name = "IsSelected";
col.HeaderText = "IsSelected";
col.ReadOnly = false;
dataGridView1.Columns.Add(col);
col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Country";
col.Name = "Country";
col.HeaderText = "Country";
col.ReadOnly = true;
dataGridView1.Columns.Add(col);
dataGridView1.DataSource = dt;
m_checked = false;
}
private bool m_checked = false;
private void btnInvertSelection_Click(object sender, EventArgs e)
{
dataGridView1.EndEdit();
m_checked = !m_checked;
foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows)
{
dataGridViewRow.Cells["IsSelected"].Value = m_checked;
}
dataGridView1.EndEdit();
DataTable dataTable = (DataTable)dataGridView1.DataSource;
DataView dataView = new DataView(dataTable);
dataView.RowFilter = "IsSelected=true";
MessageBox.Show("No of Selected Rows: " +
dataView.Count.ToString());
MessageBox.Show("RowState of First Row in DataTable: " +
dataTable.Rows[0].RowState.ToString());
}
}
}
Lior Ronen
2009-06-14 17:04:02 UTC
Permalink
Hi Cor,

Once I create the I execute AcceptChanges before binding it to the
DataGridView so I can have a clean start.

This only occurs once in the Form's Load event.
This is a Windows Application.

AcceptChanges isn't the source of the problem.

Have you been able to reproduce the problem ?

Thanks,
Lior
Post by Cor Ligthert[MVP]
Lior,
Yes I have the idea there is a bug in your program.
Are you want to set all changes to unchanged
dt.AcceptChanges();
Cor
Post by Lior Ronen
Hi,
After clicking the button The number of rows selected shows 2 instead of 3
and the RowState of the first row is Unchanged.
Is this a bug ?
Following is the code to reproduce the problem.
Just copy and paste it into a new Form.
Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <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.btnInvertSelection = 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(42, 67);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(362, 203);
this.dataGridView1.TabIndex = 0;
//
// btnInvertSelection
//
this.btnInvertSelection.Location = new System.Drawing.Point(42,
23);
this.btnInvertSelection.Name = "btnInvertSelection";
this.btnInvertSelection.Size = new System.Drawing.Size(144, 38);
this.btnInvertSelection.TabIndex = 1;
this.btnInvertSelection.Text = "Select/Unselect All";
this.btnInvertSelection.UseVisualStyleBackColor = true;
this.btnInvertSelection.Click += new
System.EventHandler(this.btnInvertSelection_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(458, 337);
this.Controls.Add(this.btnInvertSelection);
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 btnInvertSelection;
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
dc.DataType = typeof(System.Boolean);
dc.ColumnName = "IsSelected";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = typeof(System.String);
dc.ColumnName = "Country";
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["Country"] = "USA";
dr["IsSelected"] = false;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["IsSelected"] = false;
dr["Country"] = "Spain";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["IsSelected"] = false;
dr["Country"] = "Canada";
dt.Rows.Add(dr);
dt.AcceptChanges();
dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToOrderColumns = true;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.SelectionMode =
System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
this.dataGridView1.RowHeadersVisible = false;
DataGridViewColumn col = new DataGridViewCheckBoxColumn();
col.DataPropertyName = "IsSelected";
col.Name = "IsSelected";
col.HeaderText = "IsSelected";
col.ReadOnly = false;
dataGridView1.Columns.Add(col);
col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Country";
col.Name = "Country";
col.HeaderText = "Country";
col.ReadOnly = true;
dataGridView1.Columns.Add(col);
dataGridView1.DataSource = dt;
m_checked = false;
}
private bool m_checked = false;
private void btnInvertSelection_Click(object sender, EventArgs e)
{
dataGridView1.EndEdit();
m_checked = !m_checked;
foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows)
{
dataGridViewRow.Cells["IsSelected"].Value = m_checked;
}
dataGridView1.EndEdit();
DataTable dataTable = (DataTable)dataGridView1.DataSource;
DataView dataView = new DataView(dataTable);
dataView.RowFilter = "IsSelected=true";
MessageBox.Show("No of Selected Rows: " +
dataView.Count.ToString());
MessageBox.Show("RowState of First Row in DataTable: " +
dataTable.Rows[0].RowState.ToString());
}
}
}
Cor Ligthert[MVP]
2009-06-15 06:30:37 UTC
Permalink
Lior,

Sorry,

I am currently also active in the forums, there persons paste in simply a
bunch of s...t and ask than what is the problem with the code, forcing
sometimes even that you create a whole program for them.

I tried your nice error reproducable code, this was a bug wherefore the
BindingSource is created as a kind of intermidiate (It does more now)

I changed some code in your code; now it gives in my idea the wanted
results.

I hope this helps,

Cor

\\\
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
m_checked = false;
}

private bool m_checked = false;
private void btnInvertSelection_Click(object sender, EventArgs e)
{
dataGridView1.EndEdit();

m_checked = !m_checked;

foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows)
{
dataGridViewRow.Cells["IsSelected"].Value = m_checked;
}
BindingSource bs = (BindingSource)dataGridView1.DataSource;
bs.EndEdit();

DataTable dataTable = (DataTable)bs.DataSource;
DataView dataView = new DataView(dataTable);
///

Loading...