468,734 Members | 2,298 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,734 developers. It's quick & easy.

Selecting current row in DataGrid after sorting

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

Nov 16 '05 #1
6 13328
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
<aa*@bbb.hr> wrote in message news:cg**********@garrison.globalnet.hr...
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

Nov 16 '05 #2
aa*@bbb.hr wrote:
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;
}

--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)
Nov 16 '05 #3
aaa
John E. wrote:
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...

Nov 16 '05 #4
aaa
Ken Arway wrote:
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.BindingCo ntext[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

Nov 16 '05 #5
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;
<aa*@bbb.hr> wrote in message news:cg**********@garrison.globalnet.hr...
Ken Arway wrote:
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.BindingCo ntext[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

Nov 16 '05 #6
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.d g)).BeginInit();
this.SuspendLayout();
//
// dg
//
this.dg.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Win dows.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_Mou seUp);
//
// 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.d g)).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;
}
}
}
}
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Deep Silent Ocean | last post: by
3 posts views Thread by John Smith | last post: by
5 posts views Thread by PJHORNSA | last post: by
2 posts views Thread by PJHORNSA | last post: by
2 posts views Thread by Simon | last post: by
1 post views Thread by Jeremy | last post: by
2 posts views Thread by saleek | last post: by
2 posts views Thread by Kuldeep | last post: by
1 post views Thread by richard | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.