Identifying a newly created row ?

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

Guest

Hi

I have a DataGrid bound to a DataView, and when adding a new row to the view I want it to be the current/selected one
I've tried the followerin

DataRowView row = myView.AddRow()
row["Date"] = DateTime.Today
 
Hi Jorgen,

You might iterate through DataView and compare the row to find the right
index:

for (int i=0, i<myView.Count; i++)
if (row == myView)
{
myGrid.Select(i);
break;
}


--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

JorgenD said:
Hi.

I have a DataGrid bound to a DataView, and when adding a new row to the
view I want it to be the current/selected one.
I've tried the followering

DataRowView row = myView.AddRow();
row["Date"] = DateTime.Today;
.
.
row.EndEdit();
myGrid.Select(myView.Count-1);

This works fine as long the view dosn't have the Sort-property set, but it is set like this:
myView.Sort="Date";
Now, when adding a new row with the column "Date" set to a date before the
date of the last row in the view, the new row won't be at position
"myView.Count-1"
 
Thanks for Miha's quick response,

Hi Jorgen,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you would like to select a row which
has just been created on the datagrid. If there is any misunderstanding,
please feel free to let me know.

Miha has provided us with a good suggestion. Besides, we can also use
CurrencyManager to achieve this. CurrencyManager class is used to manage a
list of Binding objects. We can also use it to add new rows to data source.
Here I've written a code snippet according to your needs.

DataView dv = (DataView)this.dataGrid1.DataSource;
CurrencyManager cm = (CurrencyManager)this.BindingContext[dv];
cm.AddNew();
DataRowView row = (DataRowView)cm.Current;
row["Date"] = DateTime.Today;
row.EndEdit();
this.dataGrid1.Select(cm.Position);

For more information about CurrencyManager, here is a link for your
reference:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscurrencymanagerclasstopic.asp

Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top