c# Winforms DataGrid: What's the difference between highlighted and selected rows ???

B

BBFrost

Win2000
..Net 1.1 SP1
c# using Visual Studio

Ok, I'm currently in a "knock down - drag out" tussle with the .Net 1.1
datagrid.

I've come to realize that a 'block' of rows highlighted within a datagrid doone row may be 'selected' in a datagrid. By selected row I mean the row
pointer within the datagrid datasource.

I was trying to set up a process that would allow the user to select a set
of rows in DataGrid #1 and then mash a button that would transfer those rows
to DataGrid #2. The rows would then be removed from DataGrid #1.

I've tried a couple iterations of the following code.


1st try ... this code simply moved the record at the datagrid record pointer
and ignored the other highlighted records. Apparently the dv.Delete( );
statement somehow unhighlights the rest of the datagrid records.


//' DERIVE the source table for the Search Results Grid.
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dgSearchResults.DataSource,this.dgSearchResults.DataMember];

DataView dv = (DataView)cm.List;

for(int i=0; i < dv.Count; ++i)
{
if(this.dgSearchResults.IsSelected(i))
{
mDs.Tables[this.mSelectedDirectoryIDsTableName].Rows.Add
(new Object[] {
dv["directory_id"],
dv["display_name"],
dv["street1"],
dv["city"],
dv["last_updt_date"]
});
dv.Delete( );
}
}


2nd try ...

This example moves all of the selected datagrid #1 records to datagrid #2,
deletes the datagrid #1 record designated by the datagrid #1 record pointer
and leaves the other 'formerly highlighted' records in datagrid #1 and
unhighlighted. This is stronger proof that dv.Delete( ); causes the
records in datagrid #1 to be unselected.

//' DERIVE the source table for the Search Results Grid.
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dgSearchResults.DataSource,this.dgSearchResults.DataMember];

DataView dv = (DataView)cm.List;

for(int i=0; i < dv.Count; ++i)
{
if(this.dgSearchResults.IsSelected(i))
{
mDs.Tables[this.mSelectedDirectoryIDsTableName].Rows.Add
(new Object[] {
dv["directory_id"],
dv["display_name"],
dv["street1"],
dv["city"],
dv["last_updt_date"]
});
}
}

for(int i=0; i < dv.Count; ++i)
{
if(this.dgSearchResults.IsSelected(i))
{
dv.Delete( );
}
}

The only solution I can see here is to pass all the highlighted datagrid #1
records and 'tag' them in some way; then write a method to hunt the tagged
records down, move them to datagrid #2 and then delete them from datagrid
#1.

After working with c# for more than 2 years I still find myself stunned at
how much of a PIA seemingly simple processes like this can be. I'm REALLY
hoping some of you wizards out there have a better / more elegant solution
than this. I've got to admit that even after all this time the Currency
Manager mechanism is still a mystery to me ... if it wasn't for help from
this group I'd have been sunk long ago.

Thanks in advance.

Barry
in Oregon

(e-mail address removed)
 
K

Kevin Yu [MSFT]

Hi Barry,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to transfer the selected rows
from DataGrid #1 to DataGrid #2 when a button is clicked. If there is any
misunderstanding, please feel free to let me know.

As you know, dv.Delele() will deselect all the other rows. So we cannot
use that directly. I think we can fist go through all the rows in the grid
and use DataGrid.IsSelected(int) method to decide which rows have been
selected. We put the indexes to an array. Then move the rows according to
the array.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
B

BBFrost

Kevin,

Thanks for your prompt reply, its very much appreciated. I was hoping that
the datagrid had a better method for tracking when a user highlights a
different set of rows within a datagrid. Oh, well.

With your help here and your answer to my "c# Winforms DataGrid: What's the
difference between highlighted and selected rows ???" question I think I can
set up a mechanism that will allow me to accomplish what I'm trying to do.

Thanks again.

Barry
in Oregon
 
K

Kevin Yu [MSFT]

Hi Barry,

I think the names of methods lead to misunderstanding. Actually Selected
means the row that mouse is pushed down. The all the rows with blue
backgroud color are highlighted. IsSelected method actually mean
IsHighlighted.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
B

BBFrost

Thanks Kevin,

That's the understanding that I finally reached. I finally ended up passing
the data grid 2 times. The 1st time I tagged the 'highlighted' rows
and the 2nd pass I operated on the 'tagged' rows.

Best wishes.

Barry
in Oregon
 
K

Kevin Yu [MSFT]

Hi Barry,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top