473,732 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
datagridtablest yle defined. I use the datagridtablest yle 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.HitTes tInfo 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 BindingManagerB ase like this:

bm = Me.dg.BindingCo ntext(Me.dg.Dat aSource)

However, it seems that the datarow accessed by ctype(bm.curren t,
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 = ConvertToDataTa bleRow(RowInDat aGrid).

I would appreciate any guidance on this.
Thanks, John


Nov 21 '05 #1
10 4950
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 datagridtablest yle).

John

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP15.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.defaultvi ew.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.sor t 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.sor t 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 programmaticall y?

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******** ******@TK2MSFTN GP10.phx.gbl...
John,

Just have a look at the table.defaultvi ew.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******** *******@TK2MSFT NGP15.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da As OleDbDataAdapte r

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"

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

conn = New OleDbConnection (strConn)

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

da.Fill(ds, "Categories ")

DataGrid1.DataS ource = ds.Tables("Cate gories")

End Sub

Private Sub Form1_DoubleCli ck(ByVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.DoubleCl ick

Dim cm As CurrencyManager = CType(Me.Bindin gContext(DataGr id1.DataSource) ,
CurrencyManager )

Dim drv As DataRowView

drv = ds.Tables("Cate gories").Defaul tView.Item(cm.P osition)

MessageBox.Show (drv.Item("Cate goryName").ToSt ring)

End Sub

Ken
--------------------
"JohnR" <Jo******@hotma il.com> wrote in message
news:WnTke.391$ Qd3.256@trndny0 1...
I have a datatable as the datasource to a datagrid. The datagrid has a
datagridtablest yle defined. I use the datagridtablest yle 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.HitTes tInfo 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 BindingManagerB ase like this:

bm = Me.dg.BindingCo ntext(Me.dg.Dat aSource)

However, it seems that the datarow accessed by ctype(bm.curren t,
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 = ConvertToDataTa bleRow(RowInDat aGrid).

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******@hotma il.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******** *******@TK2MSFT NGP15.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 GetDataTableCol Num(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.Inde xOf(dg.TableSty les.Item(0).Gri dColumnStyles.I tem(GridCol).Ma ppingName)

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

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

Similar topics

2
1804
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 that displays summary data - other controls (eg textboxes, comboboxes) that are editable
3
2603
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. Thanks
4
5441
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; Of course, I get completely the wrong row of data if the user has sorted the table by clicking any of the column headers.
4
2841
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 form/grid first opens up and the grid is immediately sorted the TB don't reflect the sorted data of the First row of the grid. Note: Initially the black grid indicator arrow points to the first row. If the user choses another row and then sorts...
1
1601
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 well asigned. The DataMenber assigned to the table. One column of the DataSet is decimal type.
0
1384
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 datagridForm from my mainForm:
2
6718
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 event, I have a DataGrid, added a GridTableStyle, bound a DataTable to it and everything displays pretty well. Now, sometimes when the user clicks on a cell I would like to be able to determine the row/column number and set the foreground and...
7
3117
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 colums...something like textbox = contents (rx, cx) How can I refer to the cell contents? Thanks....
12
13642
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 display in a textbox below the datgridview. I currently have it fixed so if you click on a row the note field from that row is put in the textbox. I'd like this to be automatic as they move through rows. Also the note field is one of two fields...
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9306
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.