Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-14
3
Medium Priority
?
900 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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…

715 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