DBGrid, CurrentRow. Easier way?

  • Thread starter Chad Z. Hower aka Kudzu
  • Start date
C

Chad Z. Hower aka Kudzu

CurrentSelectedIndex of course wont work - user can resort rows by clicking
on a column title and then index of the grid won't reflect the same order as
the bound data table.

Currently the code is doing this:

CurrencyManager xCM = (CurrencyManager)BindingContext[dgrdVendors.DataSource,
dgrdVendors.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
_Vendor = (AdminWS.VendorListDS.VendorRow)xDRV.Row;

Just to get the matching row in the datatable for the currently selected row
in the datagrid. Is there an easier / shorter way I've overlooked?


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
How are you, we did a long time not see you here, (two days ago I saw
you again). For this you need the and the bindingmanager position.

Thanks. Just been busy. :)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/ht
ml/frlrfsystemwindowsformsbindingmanagerbaseclasspositiontopic.asp

or currencymanager position
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/ht
ml/frlrfsystemwindowsformscurrencymanagerclasspositiontopic.asp

Not quiet.. The second one is a descendant of the first class, and merely
is an override.

Here is what we are using now - it does work even if the user resorts the
grid:
CurrencyManager xCM = (CurrencyManager)BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
AdminWS.AdminCartReadDS.CartRow xCart
= (AdminWS.AdminCartReadDS.CartRow)xDRV.Row;

However this is a lot of work just to get the current row. Im hoping there
is a shorter way - and that its not just another example of .NET being
overengineered without the appropriate "practical" short cut being added.

According to your post, this should work:
CurrencyManager xCM = (CurrencyManager)BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];
AdminWS.AdminCartReadDS.CartRow xCart
= ((AdminWS.AdminCartReadDS.CartDataTable)dataGrid1.DataSource)
[xCM.Position];

But:
1) Thats still way to much to get the current row.
2) It works - only until the user sorts the grid, which is the same result
as using the position from the grid directly. Unsorted it works -but not
sorted. Sorted rearranges the grid - but not the datatable of course.
CurrencyManager.Position seems to refer to the same as the grid row index.

There SHOULD be something like
RowType xRow = (RowType)grid.something.currentrow

There probably isnt - although there should be. But even in .NET there
should be SOMETHING shorter than what we are currently doing.
By the way the clicked sorted column is reflected in the
defaultview.sort property

Yes, but unless I resort the underlying datatable.. which is not only hack,
but has bad side effects.
Quiet easy to use.

Is it? What am I missing? :)


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
D

Dmitriy Lapshin [C# / .NET MVP]

Chad,

Your approach with DataRowView is absolutely correct. Yes, it is cumbersome,
but it is the best working one. You can create an inherited control which
would implement a kind of 'CurrentDataRow' property based on your code
snippet.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Chad Z. Hower aka Kudzu said:
Cor Ligthert said:
How are you, we did a long time not see you here, (two days ago I saw
you again). For this you need the and the bindingmanager position.

Thanks. Just been busy. :)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/ht
ml/frlrfsystemwindowsformsbindingmanagerbaseclasspositiontopic.asp

or currencymanager position
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/ht
ml/frlrfsystemwindowsformscurrencymanagerclasspositiontopic.asp

Not quiet.. The second one is a descendant of the first class, and merely
is an override.

Here is what we are using now - it does work even if the user resorts the
grid:
CurrencyManager xCM =
(CurrencyManager)BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
AdminWS.AdminCartReadDS.CartRow xCart
= (AdminWS.AdminCartReadDS.CartRow)xDRV.Row;

However this is a lot of work just to get the current row. Im hoping there
is a shorter way - and that its not just another example of .NET being
overengineered without the appropriate "practical" short cut being added.

According to your post, this should work:
CurrencyManager xCM =
(CurrencyManager)BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];
AdminWS.AdminCartReadDS.CartRow xCart
= ((AdminWS.AdminCartReadDS.CartDataTable)dataGrid1.DataSource)
[xCM.Position];

But:
1) Thats still way to much to get the current row.
2) It works - only until the user sorts the grid, which is the same result
as using the position from the grid directly. Unsorted it works -but not
sorted. Sorted rearranges the grid - but not the datatable of course.
CurrencyManager.Position seems to refer to the same as the grid row index.

There SHOULD be something like
RowType xRow = (RowType)grid.something.currentrow

There probably isnt - although there should be. But even in .NET there
should be SOMETHING shorter than what we are currently doing.
By the way the clicked sorted column is reflected in the
defaultview.sort property

Yes, but unless I resort the underlying datatable.. which is not only
hack,
but has bad side effects.
Quiet easy to use.

Is it? What am I missing? :)


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
C

Cor Ligthert

Kudzu,

Does this sample that I made for you show what is in my opinion an easier
way, works when the user pushes the sort in the datagrid as well.. Try it?

\\\needs a datagrid and a button. The load event is to fill the datatable.
private void Form2_Load(object sender,
System.EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("kudzu");
for (int i = 0;i<10;i++)
{
dt.Rows.Add(dt.NewRow());
dt.Rows["kudzu"] = i.ToString();
}
dataGrid1.DataSource = dt.DefaultView;
}
private void button1_Click(object sender,
System.EventArgs e)
{
DataRowView xDRV = ((DataView)dataGrid1.DataSource)
[((CurrencyManager)BindingContext
[dataGrid1.DataSource]).Position];

MessageBox.Show(xDRV[0].ToString());
}

I hope this helps a little bit?

Cor
 
C

Chad Z. Hower aka Kudzu

Dmitriy Lapshin said:
Your approach with DataRowView is absolutely correct. Yes, it is
cumbersome, but it is the best working one. You can create an inherited

This really sucks. This and many other items unfortunately seems to
demonstrate those at MS dreaming this stuff up arent actually using it in
real practice....
control which would implement a kind of 'CurrentDataRow' property based
on your code snippet.

Yes, thats what I planned to do. We shouldnt have to do such things for such
trivial functionality that should be native though.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
Does this sample that I made for you show what is in my opinion an easier
way, works when the user pushes the sort in the datagrid as well.. Try
DataRowView xDRV = ((DataView)dataGrid1.DataSource)
[((CurrencyManager)BindingContext
[dataGrid1.DataSource]).Position];

Its not much shorter unfortunately. Its still like taking a 747 to the
grocery store for milk...


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
C

Cor Ligthert

Kudzu,
Its not much shorter unfortunately. Its still like taking a 747 to the
grocery store for milk...

It is *shorter* and works while the user pushes the sort header.

I miss the relation to a 747. Why do you want to use that to go to a grocery
store for milk.

You told you was now in the US. You don't need that there, you just can go
to a shop mostly by car however sometimes by feet.

:)

Cor
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
It is *shorter* and works while the user pushes the sort header.

Not much shorter - the other version can be smashed up into one line too.
;)
I miss the relation to a 747. Why do you want to use that to go to a
grocery store for milk.

Exactly. Doing so is like trying to get the current data row for a grid in
WinForms. :) It should be much easier...
You told you was now in the US. You don't need that there, you just can
go to a shop mostly by car however sometimes by feet.

Yes still in the US visiting - really home sick but family situation
requires me to stay some more.

He - you have no idea... I cant stand it here. I like to drive - but I
really HATE driving EVERYWHERE. I like it much better at home where I only
need a car occasionally and not for daily shopping... Aside from New York
or so, American cities arent built for walking....

In fact, compared to Europe getting a loaf of bread here is like getting a
current row from WinForms. :)




--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 

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

Similar Threads

Getting the current row of a datagrid 2
Datagrid - Current row 6
Extending C# 4
Implicit overloads, non static 25
Current Exception 14
VB.NET vs VB 8
"What is better" question ... 6
Formatting databound values 7

Top