473,289 Members | 1,848 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,289 software developers and data experts.

translate current cell (row, column) in sorted datagrid to corresponding row/col in datatable

I have a datatable as the datasource to a datagrid. The datagrid has a
datagridtablestyle defined. I use the datagridtablestyle to change the
order of the columns (so they can be different than the column order of the
datatable). I also allow the user to click on a column header to sort the
datagrid by that column.

I need to identify the row and column in the datatable when the user clicks
on a cell in the datagrid.

Using the datagrid.HitTestInfo I can easily find out the row and column of
the datagrid (ie: hti.row and hti.column). Now I need to find the
corresponding row and column for the cell the user clicked on in the actual
datatable . I need to do this in the MOUSEDOWN event.

As an initial thought, I used a Dim bm as BindingManagerBase like this:

bm = Me.dg.BindingContext(Me.dg.DataSource)

However, it seems that the datarow accessed by ctype(bm.current,
datarowview) points to the row just before the user clicked the mouse on
the new cell. The mousedown event seems to take control before the
bm.current row is updated.

Anyway, all this means that I am now totally confused. Clearly there must
be a procedure to translate the datagrid row/column to the datatable
row/column because you can rearrange columns and sort them to your hearts
content, and when you then change the value of a cell in the datagrid, it
updates the proper row/column in the datatable. SO HOW DOES IT DO IT???

Is there some sort of function or formula that will return the datatable
row/column given the datagrid row/column??? It would be great to have
something like: RowInDatatable = ConvertToDataTableRow(RowInDataGrid).

I would appreciate any guidance on this.
Thanks, John


Nov 21 '05 #1
10 4893
John,

Every datatable has a defaultview. (A dataview). In that is the Sort
property. When there is no extra dataview used to show the datatable, than
that defaultview is used.

Therefore you can than see in the sort property which column and in what
order it is sorted.

The datatable it self is not sorted by the way.

I hope this helps,

Cor
Nov 21 '05 #2
Hi Cor,

So how would that help me to translate datagrid cell coordinates to the
datatable coordinates? Remember, the columns in the datagrid may be
switched also (via the datagridtablestyle).

John

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
John,

Every datatable has a defaultview. (A dataview). In that is the Sort
property. When there is no extra dataview used to show the datatable, than
that defaultview is used.

Therefore you can than see in the sort property which column and in what
order it is sorted.

The datatable it self is not sorted by the way.

I hope this helps,

Cor

Nov 21 '05 #3
John,

Just have a look at the table.defaultview.sort using debuging and quick
view, than you see exactly how it happens. Assuming that that is the
datasource from your datagrid.

http://msdn.microsoft.com/library/de...ssorttopic.asp

I hope this helps,

Cor
Nov 21 '05 #4
Hi Cor,

I looked at the defaultview.sort in my datatable. I see that the value
for the sort is "" if I have not yet sorted by any column, and once I click
on a column to sort, the defaultview.sort becomes the columnname that I
sorted on.

Sorry if I'm a little slow on this, but how does that help me with my
problem? I just don't see the connection. Even if I know what column is the
sort column, how do I use that information to map a datagrid cell to its
corresponding datatable row/col??? So the user clicks in the datagrid on
column 3 and row 4, and I, by examining the datatable I know that it
corresponds to column 1, row 2 in the datatable. So how do I make that
translation programmatically?

This seems like such a simple thing to want to do, and after hours of
searching for a solution I found plenty of users asking the same question.
All of the suggested solutions seemed very indirect and may not work in
every situation (like using the bm.current method I referred to in the orig
post).

I can't help but believe there has to be an easier, more direct way to do
this mapping... Any other help graciously accepted.

John

"Cor Ligthert" <no************@planet.nl> wrote in message
news:u3**************@TK2MSFTNGP10.phx.gbl...
John,

Just have a look at the table.defaultview.sort using debuging and quick
view, than you see exactly how it happens. Assuming that that is the
datasource from your datagrid.

http://msdn.microsoft.com/library/de...ssorttopic.asp

I hope this helps,

Cor

Nov 21 '05 #5
John,

You were asking in my opinon two questions, how is the sort order done and
how you can get from your datagrid to your cel in the datatable. The first I
could answer the second is for me a kind of rediculous, it is the same for
me as asking how you can use a plane to sail over the oceans. A datagrid is
not build for that, it is not a stand alone grid.

You cannot translate the current row/cell in a datagrid to a current/cell in
a datatable.

A datagrid is not a classic grid. It is a table of rows which has shows
items. Using the column mapping the right cell is found. However changes in
data goes row by row after that the user has commited by a row change that
he has done that row. (Or a button which has an endcurrentedit behind it).

In other words a datagrid uses rows and items from a datasource, which is
placed using the mapping on the screen.

You can not see in the datasource what the UI is doing. Like that you cannot
see in a datagrid what the datasource is doing. Until that both have done
what they have to do.

You can however see what is the currentrow in the datagrid. For that is the
currencymanager.

http://msdn.microsoft.com/library/de...classtopic.asp

You told that you have seen a lot of questions from people who wants to use
a Grid in dotNet on the classic way. Mostly that people are after a while
glad that there is now a better datagrid.

However just my thought,

Cor

Nov 21 '05 #6
Hi Cor,

Well, actually, I have a very good reason (I believe) to be able to track
the datagrid row/col back to the datatable and here it is...

I actually have two datatables that mirror each other. Let's say they both
have columns of "FEATURE" and "VALUE". In the first datatable the VALUE
column is of type "string". In the second datatable the VALUE is of type
"object". I use the first datatable as a datasource for the datagrid, and
the grid displays strings in each of the columns. Some sample rows might be
something like:

FEATURE VALUE
Security Yes
Combinations <click for more info>
Last Access 5/4/2005

Notice the <click for more info> text in the 2nd row, 2nd col. The
corresponding row/col in the 2nd datatable (the one that stores the VALUE
column as type 'object') is an instance of a custom class.

Now, here's the fun part. When the user clicks on the cell that has the
string <click for more info> I then go to the corresponding row/col in the
2nd datatable and get the object that has the instance of the custom class
and then process it.

When the datagrid is not sorted and the columns and rows correspond exactly
to the datatable this process works perfectly.

BUT, if the datagrid is sorted and/or column are rearranged, then I need the
row/col of the cell that maps back into the datatable so that I can fetch
the corresponding OBJECT in the 2nd datatable. This is why I need to know
the mapping of grid row/col to datatable row/col.

So, I hope you can see that the requirement is actually not ridiculous...
there was some serious thought behind it.

Regards,
John



"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
John,

You were asking in my opinon two questions, how is the sort order done and
how you can get from your datagrid to your cel in the datatable. The first
I could answer the second is for me a kind of rediculous, it is the same
for me as asking how you can use a plane to sail over the oceans. A
datagrid is not build for that, it is not a stand alone grid.

You cannot translate the current row/cell in a datagrid to a current/cell
in a datatable.

A datagrid is not a classic grid. It is a table of rows which has shows
items. Using the column mapping the right cell is found. However changes
in data goes row by row after that the user has commited by a row change
that he has done that row. (Or a button which has an endcurrentedit behind
it).

In other words a datagrid uses rows and items from a datasource, which is
placed using the mapping on the screen.

You can not see in the datasource what the UI is doing. Like that you
cannot see in a datagrid what the datasource is doing. Until that both
have done what they have to do.

You can however see what is the currentrow in the datagrid. For that is
the currencymanager.

http://msdn.microsoft.com/library/de...classtopic.asp

You told that you have seen a lot of questions from people who wants to
use a Grid in dotNet on the classic way. Mostly that people are after a
while glad that there is now a better datagrid.

However just my thought,

Cor

Nov 21 '05 #7
Hi,

Use the datatables default view to return right datarow. Quick
example.
Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

DataGrid1.DataSource = ds.Tables("Categories")

End Sub

Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.DoubleClick

Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource),
CurrencyManager)

Dim drv As DataRowView

drv = ds.Tables("Categories").DefaultView.Item(cm.Positi on)

MessageBox.Show(drv.Item("CategoryName").ToString)

End Sub

Ken
--------------------
"JohnR" <Jo******@hotmail.com> wrote in message
news:WnTke.391$Qd3.256@trndny01...
I have a datatable as the datasource to a datagrid. The datagrid has a
datagridtablestyle defined. I use the datagridtablestyle to change the
order of the columns (so they can be different than the column order of the
datatable). I also allow the user to click on a column header to sort the
datagrid by that column.

I need to identify the row and column in the datatable when the user clicks
on a cell in the datagrid.

Using the datagrid.HitTestInfo I can easily find out the row and column of
the datagrid (ie: hti.row and hti.column). Now I need to find the
corresponding row and column for the cell the user clicked on in the actual
datatable . I need to do this in the MOUSEDOWN event.

As an initial thought, I used a Dim bm as BindingManagerBase like this:

bm = Me.dg.BindingContext(Me.dg.DataSource)

However, it seems that the datarow accessed by ctype(bm.current,
datarowview) points to the row just before the user clicked the mouse on
the new cell. The mousedown event seems to take control before the
bm.current row is updated.

Anyway, all this means that I am now totally confused. Clearly there must
be a procedure to translate the datagrid row/column to the datatable
row/column because you can rearrange columns and sort them to your hearts
content, and when you then change the value of a cell in the datagrid, it
updates the proper row/column in the datatable. SO HOW DOES IT DO IT???

Is there some sort of function or formula that will return the datatable
row/column given the datagrid row/column??? It would be great to have
something like: RowInDatatable = ConvertToDataTableRow(RowInDataGrid).

I would appreciate any guidance on this.
Thanks, John

Nov 21 '05 #8
JohnR,

Maybe can this helps you in future, I do really know nothing from it.

http://msdn.microsoft.com/library/de...tml/vs05a9.asp
Cor



"JohnR" <Jo******@hotmail.com> schreef in bericht
news:RH4le.346$Fb.106@trndny07...
Hi Cor,

Well, actually, I have a very good reason (I believe) to be able to
track the datagrid row/col back to the datatable and here it is...

I actually have two datatables that mirror each other. Let's say they
both have columns of "FEATURE" and "VALUE". In the first datatable the
VALUE column is of type "string". In the second datatable the VALUE is of
type "object". I use the first datatable as a datasource for the
datagrid, and the grid displays strings in each of the columns. Some
sample rows might be something like:

FEATURE VALUE
Security Yes
Combinations <click for more info>
Last Access 5/4/2005

Notice the <click for more info> text in the 2nd row, 2nd col. The
corresponding row/col in the 2nd datatable (the one that stores the VALUE
column as type 'object') is an instance of a custom class.

Now, here's the fun part. When the user clicks on the cell that has the
string <click for more info> I then go to the corresponding row/col in the
2nd datatable and get the object that has the instance of the custom class
and then process it.

When the datagrid is not sorted and the columns and rows correspond
exactly to the datatable this process works perfectly.

BUT, if the datagrid is sorted and/or column are rearranged, then I need
the row/col of the cell that maps back into the datatable so that I can
fetch the corresponding OBJECT in the 2nd datatable. This is why I need
to know the mapping of grid row/col to datatable row/col.

So, I hope you can see that the requirement is actually not ridiculous...
there was some serious thought behind it.

Regards,
John



"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
John,

You were asking in my opinon two questions, how is the sort order done
and how you can get from your datagrid to your cel in the datatable. The
first I could answer the second is for me a kind of rediculous, it is the
same for me as asking how you can use a plane to sail over the oceans. A
datagrid is not build for that, it is not a stand alone grid.

You cannot translate the current row/cell in a datagrid to a current/cell
in a datatable.

A datagrid is not a classic grid. It is a table of rows which has shows
items. Using the column mapping the right cell is found. However changes
in data goes row by row after that the user has commited by a row change
that he has done that row. (Or a button which has an endcurrentedit
behind it).

In other words a datagrid uses rows and items from a datasource, which is
placed using the mapping on the screen.

You can not see in the datasource what the UI is doing. Like that you
cannot see in a datagrid what the datasource is doing. Until that both
have done what they have to do.

You can however see what is the currentrow in the datagrid. For that is
the currencymanager.

http://msdn.microsoft.com/library/de...classtopic.asp

You told that you have seen a lot of questions from people who wants to
use a Grid in dotNet on the classic way. Mostly that people are after a
while glad that there is now a better datagrid.

However just my thought,

Cor


Nov 21 '05 #9
OK, here's the solution I implemented. Remember, the problem was that in a
datagrid with a datatable as a datasource, I needed to find the row/col in
the datatable given the row/col in the datagrid. This needed to work even
if the grid columns were rearranged, and if the columns were sorted.

The first problem was the easiest. how to find the column. This is how I
did it:

Public Function GetDataTableColNum(ByVal GridCol As Integer, ByVal dg As
DataGrid, ByVal dt As DataTable) As Integer

If dg.TableStyles.Count = 0 Then

Return GridCol

Else

Return
dt.Columns.IndexOf(dg.TableStyles.Item(0).GridColu mnStyles.Item(GridCol).MappingName)

End If

End Function

This assumes that the datagrid has either 0 or 1 associated tablestyle.

The problem of a sorted column was a little more complicated. Using a
DataView (either a user defined one, or the DefaultView) you can return a
DATAROW object from the datatable (not the row number, but the actual row)
this way:

Public Function GetDataTableRow(ByVal GridRow As Integer, ByVal dg As
DataGrid, ByVal dt As DataTable, Optional ByVal dv As DataView = Nothing) As
DataRow

Dim drv As DataRowView

If dv Is Nothing Then

drv = dt.DefaultView(GridRow)

Else

drv = dv(GridRow)

End If

Return drv.Row

End Function

For most users this would suffice. However, since I was generating 2
datatables at runtime I actually needed a row number so I could fetch info
from the datatable that was not currently linked to the grid. The data in
the 2 datatables had 'related' information for each row/col cell. So row 1,
col 1 in datatable 1 was related to row 1, col1 in datatable 2.

Anyway, since the ROW object has no reference to the rownumber, I simply
added a "rownumber" column to the datatable that was linked to the grid. As
I built the datatable, I incremented the rownumber for each row.

Therefore, when I used the above code to retrieve the DATAROW object from
the datatable, I simply obtained the actual row number from the rownumber
column. I then used that number, plus the column number obtained
previously, to get the proper row/col in the second datatable.

Thanks to all that offered advice.

John
Nov 21 '05 #10
Hi Cor,

Thanks for the link... The new grid looks great. Can't wait for the
..NET 2005 release.

John
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
JohnR,

Maybe can this helps you in future, I do really know nothing from it.

http://msdn.microsoft.com/library/de...tml/vs05a9.asp
Cor



"JohnR" <Jo******@hotmail.com> schreef in bericht
news:RH4le.346$Fb.106@trndny07...
Hi Cor,

Well, actually, I have a very good reason (I believe) to be able to
track the datagrid row/col back to the datatable and here it is...

I actually have two datatables that mirror each other. Let's say they
both have columns of "FEATURE" and "VALUE". In the first datatable the
VALUE column is of type "string". In the second datatable the VALUE is
of type "object". I use the first datatable as a datasource for the
datagrid, and the grid displays strings in each of the columns. Some
sample rows might be something like:

FEATURE VALUE
Security Yes
Combinations <click for more info>
Last Access 5/4/2005

Notice the <click for more info> text in the 2nd row, 2nd col. The
corresponding row/col in the 2nd datatable (the one that stores the VALUE
column as type 'object') is an instance of a custom class.

Now, here's the fun part. When the user clicks on the cell that has the
string <click for more info> I then go to the corresponding row/col in
the 2nd datatable and get the object that has the instance of the custom
class and then process it.

When the datagrid is not sorted and the columns and rows correspond
exactly to the datatable this process works perfectly.

BUT, if the datagrid is sorted and/or column are rearranged, then I need
the row/col of the cell that maps back into the datatable so that I can
fetch the corresponding OBJECT in the 2nd datatable. This is why I need
to know the mapping of grid row/col to datatable row/col.

So, I hope you can see that the requirement is actually not ridiculous...
there was some serious thought behind it.

Regards,
John



"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
John,

You were asking in my opinon two questions, how is the sort order done
and how you can get from your datagrid to your cel in the datatable. The
first I could answer the second is for me a kind of rediculous, it is
the same for me as asking how you can use a plane to sail over the
oceans. A datagrid is not build for that, it is not a stand alone grid.

You cannot translate the current row/cell in a datagrid to a
current/cell in a datatable.

A datagrid is not a classic grid. It is a table of rows which has shows
items. Using the column mapping the right cell is found. However changes
in data goes row by row after that the user has commited by a row change
that he has done that row. (Or a button which has an endcurrentedit
behind it).

In other words a datagrid uses rows and items from a datasource, which
is placed using the mapping on the screen.

You can not see in the datasource what the UI is doing. Like that you
cannot see in a datagrid what the datasource is doing. Until that both
have done what they have to do.

You can however see what is the currentrow in the datagrid. For that is
the currencymanager.

http://msdn.microsoft.com/library/de...classtopic.asp

You told that you have seen a lot of questions from people who wants to
use a Grid in dotNet on the classic way. Mostly that people are after a
while glad that there is now a better datagrid.

However just my thought,

Cor



Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mrstrong | last post by:
Gday, Am new to using datagrids so if anyone could assist or point in the right direction in terms of resources - that would be much appreciated... I have a form that has: - a datagrid...
3
by: Michael Schindler | last post by:
Hello NG How I can delete a specific cell in my Datagrid. I would like if the user is in the column 6 the value changed, after automaticly the value in the column 7 in the same row to delete....
4
by: Randy | last post by:
I have a DataTable in a DataGrid. If I click on the DataGrid, HitTestInfo in dataGrid1_MouseDown returns a Row and Column number. I can use the Row number to say: DataRow dr = dataTable.Rows;...
4
by: Steve B. | last post by:
I have a DataGrid on the left and TextBoxes (TB) on the right. The TB's reflect the contents of the grid cells. Sorting of columns (both thru VS and programmatically) work fine except, when the...
1
by: Raul Cortes | last post by:
Hi. I am using a windowsform datagrid to modify a table. The DataSet seems to be filled fined. The DataTable seems to be ok. I am not using 'read only' anywhere. The DataSource seems to be...
0
by: Ben | last post by:
module main ... application.run(new splashform) .. end module after a few screen, I try to load a new codes I got from MSDN on datagrid that works on its own. I took out submain and ran...
2
by: fripper | last post by:
After a good bit of urging from folks in this newsgroup I have begun to become familiar with the VB .Net DataGrid control as a replacement for one my favorite VB6 controls ... the FlexGrid. In any...
7
by: Arne Beruldsen | last post by:
in vbnet2005 I have a datagridview. When the user clicks on a row...I would like the contents of certain cells to populate a textbox. To do this...i need to be able to refer to the row and...
12
by: cj | last post by:
When viewing a datatable in a datagridview one of the columns in it is a "note" field which can be quite long. I would like to have the note field of the currently selected row of the datagrid...
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: 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:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.