Cannot get the changes dataset correctly!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

I have a problem to get the changes of the dataset in the datagrid after
sorting the datagrid.
Firstly, I change some rows in the datagrid, and sort it. Then Update it.

And i trace my program, i found that, the dataview can get the correct data,
but not the dataset.getchanges() functions

The codes are~
Dim dvMRM = dsABC.ABC.DefaultView
Dim dsMRM = dvMRM.Table.Dataset

' I try to get the dvMRM's dataset by getxml() to see, and i found that they
get the correct dataset which the value has been changed after my editing

'but when i try to get the the changes
Me.dsMRM.getChanges(DataRowState.Modified)

I found that it cannot get all the changes of the dataset!
If i edit N rows, it will only change N-1 Rows
 
OIC~~Thanks~
But, How can I send focused on a row?
Also, another question is how can I identify the action "Sorting" on the
Datagrid after I click on the Header of datagrid?
 
I find this for you(text between " **** start *** and **** end ***").
It will answer both of your question:
1. "But, How can I send focused on a row?"
Right after sort, focus will be back to current row.
2. "Also, another question is how can I identify the action "Sorting" on the
Data after I click on the Header of datagrid?"
Mouse down event in code below, contains answer to your question. It shows
how to detect click on column header. Any click to column header with
property AllowSorting = true, is "sort action".

Best wishes,
P.



**** start ***
Re-Selecting The Currently Selected Row After A DataGrid Sort


This is a follow-up to this previous post about sorting a DataGrid. It turns
out that the person who originally asked the question about knowing when the
sort would occur, wanted to know this for the purpose of re-selecting the
currently selected row after the sort. Well, as it turns out this is
entirely possible, but it requires some deeper digging into the data-binding
aspects of WinForms (which I love btw). ;)
So here's what you'll need to do. First things first, you're going to need
to watch for the sort to happen as was detailed in the previous post. Upon
detecting the sort about to happen you must record a key value for the
currently selected row in a state field (probably in your Form). Now, here's
where the deeper knowledge of data-binding comes into play. You must also
attach an event listener to the ItemChanged event of the CurrencyManager for
the DataTable/DataView you've bound your DataGrid to. The ItemChanged event
will fire after the sort takes place at which point you use the DataView's
(DataTable::DefaultView if you're bound directly to a DataTable) Find method
to locate the row in the view by the key value that you stored in your state
field in the sort detection event logic. Once you've found the new position
of the row in the DataView after the sort, you set the CurrencyManager's
Position property to that index.
Ok, this might sound complex, but lemme show you just how easy it is with
some code. For the purposes of keeping this code concise, assume this code
is in a Form subclass which contains a DataGrid in a member variable called
"myDataGrid". This DataGrid is then bound to a DataTable, stored in a member
variable called "myDataTable" which has a key column named "key" of type
int:

public class MyForm : Form
{
private int currentlySelectedRowId = -1;

public MyForm()
{
this.InitializeComponent();

// Get the CurrencyManager for the bound DataTable
CurrencyManager dataTableCurrencyManager =
(CurrencyManager)this.myDataGrid.BindingContext[this.myDataTable];

// Hook up to the ItemChanged event
dataTableCurrencyManager.ItemChanged += new
ItemChangeEventHandler(this.myDataTableCurrencyManager_ItemChanged);
}

private void myDataGrid_MouseUp(object sender, MouseEventArgs args)
{
DataGrid.HitTestInfo hitTestInfo = this.myDataGrid.HitTest(args.X,
args.Y);

if(hitTestInfo.Type == DataGrid.HitTestType.ColumnHeader)
{
// Get the CurrencyManager for the bound DataTable
CurrencyManager dataTableCurrencyManager =
(CurrencyManager)this.myDataGrid.BindingContext[this.myDataTable];

// Get the current DataRowView
DataRowView currentRowView =
(DataRowView)dataTableCurrencyManager.Current;
// Remember the currently selected row ID
this.currentlySelectedRowId = (int)currentRowView["key"];
}
}

private void myDataTableCurrencyManager_ItemChanged(object sender,
ItemChangeEventArgs args)
{
// Only execute this logic if we're in the state of sorting
if(this.currentlySelectedRowId != -1)
{
// Find the new position of the row now that it's been sorted
int newPosition =
this.myDataTable.DefaultView.Find(this.currentSelectedRowId);

// Get the CurrencyManager for the bound DataTable
CurrencyManager dataTableCurrencyManager =
(CurrencyManager)this.myDataGrid.BindingContext[this.myDataTable];

// Change the position of the currency manager
dataTableCurrencyManager.Position = newPosition;

// Reset sorting state
this.currentSelectedRowId = -1;
}
}
}

**** end ***

(source:
http://216.239.59.104/search?q=cach...2003/01/22/646.aspx+datagrid+after+sort&hl=sl)
 
Actually, I don' t know how to code the following function in VB.net
As i cannot find this event handler in the drop down list box in the visual
studio .net

private void myDataTableCurrencyManager_ItemChanged(object sender,
ItemChangeEventArgs args)
{
// Only execute this logic if we're in the state of sorting
if(this.currentlySelectedRowId != -1)
{
// Find the new position of the row now that it's been sorted
int newPosition =
this.myDataTable.DefaultView.Find(this.currentSelectedRowId);

// Get the CurrencyManager for the bound DataTable
CurrencyManager dataTableCurrencyManager =
(CurrencyManager)this.myDataGrid.BindingContext[this.myDataTable];

// Change the position of the currency manager
dataTableCurrencyManager.Position = newPosition;

// Reset sorting state
this.currentSelectedRowId = -1;
}
}
}
 
Back
Top