Solved

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

Posted on 2008-06-14
3
887 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net class 3 21
VB.Net - Not Sure if Multi-threading is What I Need 4 29
Name Space error VS2015 1 22
how to just get time from a date 6 32
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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