Selecting current row in DataGrid after sorting

A

aaa

Hi

I am trying to create a read-only DataGrid that would always have
current row selected. Currently, I am using method:

public void SelectDataGridRow(DataGrid dg)
{
if (dg.CurrentRowIndex > -1)
{
dg.Select(dg.CurrentRowIndex);
}
}

to select current row. I call this method from Click, DoubleClick and
CurrentCellChanged event handlers and it works fine. The thing is that I
can not select aproporiate row when column header is clicked (and
DataGrid sorted).

I tried to use CurrencyManager associated with my grid's DataSource
(PositionChanged and CurrentChanged events), but the code above doesn't
work from these event handlers (The handler method is called when
needed, but approporiate row is not selected).

thnx in advance
 
J

John E.

I am doing the same thing as you, and looking for a solution, also. If you
find an answer, can you please post it? I will do the same.

Thank you,
John
 
K

Ken Arway

I am trying to create a read-only DataGrid that would always have
current row selected. Currently, I am using method:

public void SelectDataGridRow(DataGrid dg)
{
if (dg.CurrentRowIndex > -1)
{
dg.Select(dg.CurrentRowIndex);
}
}

to select current row. I call this method from Click, DoubleClick and
CurrentCellChanged event handlers and it works fine. The thing is that I
can not select aproporiate row when column header is clicked (and
DataGrid sorted).

Use the BindingManagerBase on the underlying DataSet. Something like:

private void Button1_Click(object sender, System.EventArgs e) {
BindingManagerBase bm = this.dataGrid1.BindingContext[dataSet1,
"tablename"];
DataRow dr = ((DataRowView)bm.Current).Row;
}
 
A

aaa

John said:
I am doing the same thing as you, and looking for a solution, also. If you
find an answer, can you please post it? I will do the same.

Thank you,
John

Will do...
 
A

aaa

Ken said:
Use the BindingManagerBase on the underlying DataSet. Something like:

private void Button1_Click(object sender, System.EventArgs e) {
BindingManagerBase bm =
this.dataGrid1.BindingContext[dataSet1, "tablename"];
DataRow dr = ((DataRowView)bm.Current).Row;
}

Right now, i am using CurrencyManager.CurrentChanged event:

private CurrencyManager _cm;
....
// DataView is DataSource
this._cm=(CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataSource];
this._cm.CurrentChanged += new System.EventHandler(this.cmChanged);
....
private void cmChanged(object sender, EventArgs e)
{
if (dataGrid1.CurrentRowIndex > -1)
{
dataGrid1.Select(dataGrid1.CurrentRowIndex);
}
}
....

Detecting changing of selected row works fine, but the code
dataGrid1.Select doesn't do the work (the row is not selected). The same
code, called from Click, DoubleClick and CurrentCellChanged event
handlers works fine.

Do you know of some other way of selecting the current row in DataGrid
(beside of dataGrid.Select method which takes current row index as input
parameter)?

thnx
 
J

John E.

DataTable dt;
DataView dv;
DataRow dr;
DataGridCell dgc;
CurrencyManager cm;

int pos = dv.Find(x);
cm = (CurrencyManager)dg.BindingContext[dt];
cm.Position = pos;
dg.UnSelect(dg.CurrentRowIndex);
dg.Select(cm.Position);
dgc = new DataGridCell(cm.Position, 0);
dg.CurrentCell = dgc;


Ken said:
Use the BindingManagerBase on the underlying DataSet. Something like:

private void Button1_Click(object sender, System.EventArgs e) {
BindingManagerBase bm =
this.dataGrid1.BindingContext[dataSet1, "tablename"];
DataRow dr = ((DataRowView)bm.Current).Row;
}

Right now, i am using CurrencyManager.CurrentChanged event:

private CurrencyManager _cm;
...
// DataView is DataSource
this._cm=(CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataS
ource];
this._cm.CurrentChanged += new System.EventHandler(this.cmChanged);
...
private void cmChanged(object sender, EventArgs e)
{
if (dataGrid1.CurrentRowIndex > -1)
{
dataGrid1.Select(dataGrid1.CurrentRowIndex);
}
}
...

Detecting changing of selected row works fine, but the code
dataGrid1.Select doesn't do the work (the row is not selected). The same
code, called from Click, DoubleClick and CurrentCellChanged event
handlers works fine.

Do you know of some other way of selecting the current row in DataGrid
(beside of dataGrid.Select method which takes current row index as input
parameter)?

thnx
 
J

John E.

Ok, got it all. Here is a sample form that does it. It is a read-only
form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication3
{

public class Form1 : System.Windows.Forms.Form
{
string x;
int pos;
DataTable dt;
DataView dv;
DataRow dr;
BindingManagerBase bmb;
private System.Windows.Forms.DataGrid dg;

private System.ComponentModel.Container components = null;

public Form1()
{
x = string.Empty;
pos = -1;
dt = new DataTable("mytable");
dv = new DataView(dt);

InitializeComponent();

PopulateDG();

this.bmb = (BindingManagerBase)dg.BindingContext[dt];

this.dv.ListChanged += new ListChangedEventHandler(dv_ListChanged);

dv.Sort = "col1";
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dg = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
this.SuspendLayout();
//
// dg
//
this.dg.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.To
p | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dg.DataMember = "";
this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dg.Location = new System.Drawing.Point(8, 16);
this.dg.Name = "dg";
this.dg.ReadOnly = true;
this.dg.Size = new System.Drawing.Size(272, 152);
this.dg.TabIndex = 0;
this.dg.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.dg_MouseUp);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 221);
this.Controls.Add(this.dg);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}


private void PopulateDG() {
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");

dr = dt.NewRow();
dr.ItemArray = new object[3] { "row1a", "row1b", "row1c" };
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray = new object[3] { "row2a", "row2b", "row2c" };
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray = new object[3] { "row3a", "row3b", "row3c" };
dt.Rows.Add(dr);

dv.Table = dt;

dg.DataSource = dv;
}

private void dg_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e) {
DataGrid.HitTestInfo hti = dg.HitTest(e.X, e.Y);

if (hti.Type == DataGrid.HitTestType.ColumnHeader) {
bmb = (CurrencyManager)dg.BindingContext[dv];
DataRowView drv = (DataRowView)bmb.Current;
x = (string)drv["col1"];
}
}

private void dv_ListChanged(object sender, ListChangedEventArgs e) {
DataGridCell dgc;

if(x != string.Empty) {
pos = dv.Find(x);
bmb = (CurrencyManager)dg.BindingContext[dt];
bmb.Position = pos;
dg.UnSelect(dg.CurrentRowIndex);
dg.Select(bmb.Position);
dgc = new DataGridCell(bmb.Position, 0);
dg.CurrentCell = dgc;
x = string.Empty;
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top