Solved

How to reference selected datagrid rows to dataset (not current row)

Posted on 2008-06-14
3
883 Views
Last Modified: 2011-04-14
Using vb.net in Visual Studio 2003

I have a datagrid bound to a datasource in which I also add and delete rows and sort columns.  I want to be able to select multiple rows in the datagrid and then perform an action on all selected rows (i.e. updates & deletions).

I am able to correlate datagrid row to datasource row when sorting by referencing the default view, but this fails when I add or delete rows.

I know I can use the currencymanager.position to reference the current row (as http://support.microsoft.com/kb/817247 ) but this only gives the current row, not selected rows.

I think I could just save & accept changes before I undertake any actions to process additions & deletions &  resynch the datagrid & datasource view but that seems inefficient.

Is there a quick way to reference a datagrid.isSelected row with the corresponding row in the dataset or  link the currencymanager.position to the datagrid row to check if selected?

Thanks
Jon

0
Comment
Question by:WestcountryBusiness
  • 3
3 Comments
 
LVL 1

Author Comment

by:WestcountryBusiness
ID: 21785984
Ok - I've found an alternative solution, which is:

Code below assumes:
dg as Datagrid
DS as dataset
dg.datasource = ds.tables("MyTable").defaultView
and KeyField is in Col 0 of datagrid.

Dim rw As DataRow()
For i as integer = 0 to dg.DataSource.count - 1
 if dg.IsSelected(i) then _
   rw = DS.Tables("MyTable").Select("KeyField= '" & dg.item(i,0) & "'")
  'check rw.length = 1 or throw error, get or set values in rw(0)  as required
next

So now the questions I have are:

1. How efficient is the .Select method.  The table I'm working with is quite small, so probably not a problem, but would this solution cause a problem on larger datasets?

2. Is this the right way of doing this, or is there a better way?

3. This solution allows me to change the data in the dataset by changing the values in rw(), which means that rw() must be a pointer to the rows in the dataset.  I think I understand why - which is that complex datatypes are assigned by reference not value, but this is part of .Net that seems a little counter intuitive to me. (I'm used to assignment creating a new, distinct 'copy' unless I specifically ask for a pointer).

So when does a variable assignment create another instance & when not and can this be overridden?  I'm assuming there must be a good online reference explaining this, but I've not come up one.  Anyone have any links?
0
 
LVL 1

Author Comment

by:WestcountryBusiness
ID: 21786011
One other thing ...

This solution assumes that the dataset index field is also in the dataset, which is not always the case. For example, If the keyfield was an auto-increment integer, that probably wouldn't be displayed in the datagrid.

The solution is to include the keyfield in the datagrid, but use a DataGridTableStyle to set the column width to zero, so it wont display.
0
 
LVL 1

Accepted Solution

by:
WestcountryBusiness earned 0 total points
ID: 21788277
Ok - I found a good article explaining how reference & value objects work in dot.net, so all is much clearer.  

For reference, the article  is called "Understanding Value and Reference Types" and can be found here:

http://www.caublestoneink.com/index.php?option=content&task=view&id=21&PHPSESSID=516a4860168f70cc00b99e63a239a94f

So I think I've resolved my issue now, but if anyone has anything to add, please feel free.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question