Solved

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

Posted on 2008-06-14
3
894 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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