473,804 Members | 3,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reverse the order of selected items

Hi

I have a datagridview in a windows C# application and i am allowing a user
to select items from the datagridview.

I allow the user to copy the selected items to the clipboard and then if
they chose, to past to the notepad application.

But the items that are selected seem to be pasted in the reverse order than
the user selected them - for example if the user selected items 1 3 5 then
order of the items in the notepad will be 5 3 1.

the code i use is below:
string counter = dgMainView.Sele ctedRows.Count. ToString();
foreach (DataGridViewRo w row in dgMainView.Sele ctedRows)

{

sb.AppendFormat ("{0} ", row.Cells[3].Value);

sb.AppendFormat (" /* {0} - Wave {1} */", row.Cells[1].Value,
row.Cells[2].Value);

sb.Append(Envir onment.NewLine) ;
}
if (counter != "0")

{

Clipboard.SetDa ta(DataFormats. Text, sb.ToString());

// Initializes the variables to pass to the MessageBox.Show method.

string message = counter + " rows copied to clipboard.\nWou ld you like to
open Notepad\nin order to paste these items?";

string caption = "Clipboard" ;

MessageBoxButto ns buttons = MessageBoxButto ns.YesNo;

MessageBoxIcon icons = MessageBoxIcon. Information;

// Displays the MessageBox.

DialogResult OpenNotePad;

OpenNotePad = MessageBox.Show (this, message, caption, buttons, icons);

if (OpenNotePad == DialogResult.Ye s)

{

string file = Path.GetTempFil eName();

StreamWriter sw = new StreamWriter(fi le);

sw.Write(sb.ToS tring());

sw.Close();

Process.Start(" Notepad", file); ;

}

}

else

{

}

Can someone explain why the data is pasted in reverse order or suggest an
alternative way that keeps the order?

Thanks

Doug
May 4 '07 #1
2 4427
Hi,

You're creating the order in question yourself, by using this iteration:
foreach (DataGridViewRo w row in dgMainView.Sele ctedRows)
So iterating the other way round should reverse your order:

for (int i = dgMainView.Sele ctedRows.Count - 1; i >= 0; i--) {
DataGridViewRow row = dgMainView.Sele ctedRows[i];
....

My personal point of view is as always: the selection order in the
SelectedRows is not something I would rely on anyway. As far as I can see,
the docs don't promise any particular order, so if you want one, you'll
have to make sure of it somehow yourself. Or just live with what you get :)

Just some arbitrary comments below:
Clipboard.SetDa ta(DataFormats. Text, sb.ToString());

// Initializes the variables to pass to the MessageBox.Show method.

string message = counter + " rows copied to clipboard.\nWou ld you like to
open Notepad\nin order to paste these items?";
I hope you don't deliver this code to a user, do you? You'd drive me wild
with this.... I never use notepad, but I use the clipboard all the time.
string caption = "Clipboard" ;

MessageBoxButto ns buttons = MessageBoxButto ns.YesNo;

MessageBoxIcon icons = MessageBoxIcon. Information;

// Displays the MessageBox.

DialogResult OpenNotePad;

OpenNotePad = MessageBox.Show (this, message, caption, buttons, icons);

if (OpenNotePad == DialogResult.Ye s)
This would drive me wild as a programmer instead of a user :-) Talk about
verbosity! What's wrong with this line:

if (MessageBox.Sho w(this,
"Do you want ...", "Clipboard" , MessageBoxButto ns.YesNo,
MessageBoxIcon. Information) == DialogResult.Ye s) {
...

Also, MessageBoxIcon. Information doesn't seem to apply here.

Sorry for jumping all over your code :-)
Oliver Sturm
--
http://www.sturmnet.org/blog - MVP C#
May 4 '07 #2
Yes thanks Oliver

I appreciate your comments - and your assistance in changing the order of my
selected items.

This code is a 'freebie' for researchers and they are quite happy to use the
notepad and another called app as they use that as a text editor to cut and
paste other elements into a SAS application.

Again thanks, I will take your comments on board.

Doug

"Oliver Sturm [MVP C#]" <ol****@sturmne t.orgwrote in message
news:1n******** ********@sturmn et.org...
Hi,

You're creating the order in question yourself, by using this iteration:
>foreach (DataGridViewRo w row in dgMainView.Sele ctedRows)

So iterating the other way round should reverse your order:

for (int i = dgMainView.Sele ctedRows.Count - 1; i >= 0; i--) {
DataGridViewRow row = dgMainView.Sele ctedRows[i];
....

My personal point of view is as always: the selection order in the
SelectedRows is not something I would rely on anyway. As far as I can see,
the docs don't promise any particular order, so if you want one, you'll
have to make sure of it somehow yourself. Or just live with what you get
:)

Just some arbitrary comments below:
>Clipboard.SetD ata(DataFormats .Text, sb.ToString());

// Initializes the variables to pass to the MessageBox.Show method.

string message = counter + " rows copied to clipboard.\nWou ld you like to
open Notepad\nin order to paste these items?";

I hope you don't deliver this code to a user, do you? You'd drive me wild
with this.... I never use notepad, but I use the clipboard all the time.
>string caption = "Clipboard" ;

MessageBoxButt ons buttons = MessageBoxButto ns.YesNo;

MessageBoxIc on icons = MessageBoxIcon. Information;

// Displays the MessageBox.

DialogResult OpenNotePad;

OpenNotePad = MessageBox.Show (this, message, caption, buttons, icons);

if (OpenNotePad == DialogResult.Ye s)

This would drive me wild as a programmer instead of a user :-) Talk about
verbosity! What's wrong with this line:

if (MessageBox.Sho w(this,
"Do you want ...", "Clipboard" , MessageBoxButto ns.YesNo,
MessageBoxIcon. Information) == DialogResult.Ye s) {
...

Also, MessageBoxIcon. Information doesn't seem to apply here.

Sorry for jumping all over your code :-)
Oliver Sturm
--
http://www.sturmnet.org/blog - MVP C#

May 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
3758
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ------------------------------------------------------------- PEP: 323
59
4356
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The HTML version is much more readable than the ReST version. See: http://www.python.org/peps/pep-0322.html
31
3005
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The updated proposal is at: www.python.org/peps/pep-0322.html In a nutshell, it proposes a builtin function that greatly simplifies reverse iteration. The core concept is that clarity comes from specifying a sequence in a forward direction and...
19
11320
by: John Keeling | last post by:
Dear all, I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list. My results on my XP Python 2.3.4 are as follows: time_reverse 0.889999389648 time_insert 15.7750005722 Over multiple runs ... the time taken to insert at the head of a list, vs. the time taken to append to a list and then reverse it is typically 16 or 17 times longer. I would have expected the insert...
8
8072
by: Bill | last post by:
I'm trying to create a wizardlike interface using a couple listboxes. I know you've seen it before. You double click on an item in one listbox and it "moves" it to the other. I used to approach it with a table and within the table a yes/no field would determine which box it would be in. An Update statement would be fired changing it from yes to no or vice versa and then refreshing the listboxes. This doesn't seem to scale very well as...
1
361
by: | last post by:
Hi, I have a listbox called lstFolders. I select the folder names from a database and return them in ascending order. However, when I do the listbox.items.insert(0, new instance) method, the items that are supposed to come back on in ascending order are the exact opposite order. The first one is at the bottom of the list.
3
4063
by: thrill5 | last post by:
I have an xml document such as: <device> <element>first</element> <element>second</element> </device> I am using this as the source for an xslt transform that goes like <xsl:for-each select="/device/element> This is the <xsl:value-of select="."/> element.
11
16809
by: Noah | last post by:
I have a list of tuples I want to reverse the order of the elements inside the tuples. I know I could do this long-form: q = y = for i in y: t=list(t)
1
2010
by: Doug | last post by:
Hi I have a datagrid view that I have enabled a use to copy data from. However the copy seems to work in reverse. This results in the last value selected being the first copied to the clipboard, but I require the data copied to be in the order in the datagridview. Is this something I should attend to within the string writer or is there a
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.