473,241 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 13527
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Deep Silent Ocean | last post by:
Hi All I have one specific question for DataGrid In the Form Load event of the Form, I am binding data with DataGrid. On clicking Row the CurrentRowIndex is updated and it gives me correct...
3
by: John Smith | last post by:
Hello all: I am having some major difficulty trying to get my DataGrids to cooperate. All I wish to do is be able to select a row in a DataGrid and from the selected row know what item to bind...
5
by: PJHORNSA | last post by:
Hi, I am stuck on a problem with the datagrid control in Visual Studio 2003, using C#. I have a main form with a datagrid, then a search screen that filters the dataset from the grid. Well on...
2
by: PJHORNSA | last post by:
Hi I just want to know if there is a event for the datagrid's sorting in C#? I am developing a windows application in Visual Studio 2003. Thanx in advance for any help! Paul
2
by: Simon | last post by:
I have read most messages in regard with this topic, and have tried every measure suggested. I'm still haveing problem to get it to work. Basically, the page just reloaded as before when I clicked...
1
by: Jeremy | last post by:
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current...
2
by: saleek | last post by:
Hi, I am trying to figure out why my datagrid has stopped firing the page and sort commands. Scenario: I originally had template columns in my datagrid and had set up custom bi-directional...
2
by: Kuldeep | last post by:
Hi All, I have been trying to find some algorithms for datagrid sorting and paging. Please help Regards Kuldeep
1
by: richard | last post by:
OK, I have got bi-directional datagrid sorting down, have a created a nice sorting class. My question, how can I add sorting to more than one datagrid on the same page? Everything I have tried has...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.