Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now