DataGridView selection changes when moving data source records.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im having trouble with a DataGridView with a custom BindingList data source when rows are moved. I want to keep the currently selected row selected when rows are moved (e.g. when the data set is sorted on a column and a value changes). Ive
distilled it down to a sample application included below.

Select the cell marked "SELECT". Every time you press "button1", row index 8 will be moved to row index 1. The "SELECT" cell remains selected until you press "button1" for the 7th time.

Notice how the "SELECT" row is no longer selected. Why is this and what can I do to prevent the selected row from changing in this situation?
Thanks,
Todd


<pre class="prettyprint" style=" using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace SampleApplication
{
public partial class Form1 : Form
{
private BindList list = new BindList();

public Form1()
{
InitializeComponent();
list.Add(new Item("0", "zero"));
list.Add(new Item("SELECT", "one"));
list.Add(new Item("2", "two"));
list.Add(new Item("3", "three"));
list.Add(new Item("4", "four"));
list.Add(new Item("5", "five"));
list.Add(new Item("6", "six"));
list.Add(new Item("7", "seven"));
list.Add(new Item("8", "eight"));
list.Add(new Item("9", "nine"));

bindingSource1.DataSource = list;
}

private void button1_Click(object sender, EventArgs e)
{
list.Move(8, 1);
}

public class BindList : BindingList<Item>
{
private void FireEvent1(ListChangedEventArgs args)
{
OnListChanged(args);
}

public void Move(int origin, int desination)
{
Move1(origin, desination);
FireEvent1(new ListChangedEventArgs(ListChangedType.ItemMoved, origin, desination));
}

private void Move1(int a, int b)
{
Item i = this[a];

RemoveAt(a);
Insert(b, i);
}
}

public class Item
{
private string id;
private string name;

public string Id
{
get
{
return id;
}
set
{
id = value;
}
}

public string Name
{
get
{
return name;

}
set
{
name = value;
}
}

public Item(string id, string name)
{
this.id = id;
this.name = name;
}

public override string ToString()
{
return id + " " + name;
}
}
}
}

namespace SampleApplication
{
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.components = new System.ComponentModel.Container();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.idDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.nameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.idDataGridViewTextBoxColumn,
this.nameDataGridViewTextBoxColumn});
this.dataGridView1.DataSource = this.bindingSource1;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(271, 295);
this.dataGridView1.TabIndex = 0;
//
// idDataGridViewTextBoxColumn
//
this.idDataGridViewTextBoxColumn.DataPropertyName = "Id";
this.idDataGridViewTextBoxColumn.HeaderText = "Id";
this.idDataGridViewTextBoxColumn.Name = "idDataGridViewTextBoxColumn";
//
// nameDataGridViewTextBoxColumn
//
this.nameDataGridViewTextBoxColumn.DataPropertyName = "Name";
this.nameDataGridViewTextBoxColumn.HeaderText = "Name";
this.nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn";
//
// bindingSource1
//
this.bindingSource1.DataSource = typeof(SampleApplication.Form1.Item);
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(289, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
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(376, 319);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.BindingSource bindingSource1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridViewTextBoxColumn idDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn nameDataGridViewTextBoxColumn;
}
}[/code]
<br/>




<br/>


View the full article
 
Back
Top