473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.SelectedRows.Count.ToString();
foreach (DataGridViewRow row in dgMainView.SelectedRows)

{

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

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

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

{

Clipboard.SetData(DataFormats.Text, sb.ToString());

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

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

string caption = "Clipboard";

MessageBoxButtons buttons = MessageBoxButtons.YesNo;

MessageBoxIcon icons = MessageBoxIcon.Information;

// Displays the MessageBox.

DialogResult OpenNotePad;

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

if (OpenNotePad == DialogResult.Yes)

{

string file = Path.GetTempFileName();

StreamWriter sw = new StreamWriter(file);

sw.Write(sb.ToString());

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 4377
Hi,

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

for (int i = dgMainView.SelectedRows.Count - 1; i >= 0; i--) {
DataGridViewRow row = dgMainView.SelectedRows[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.SetData(DataFormats.Text, sb.ToString());

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

string message = counter + " rows copied to clipboard.\nWould 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";

MessageBoxButtons buttons = MessageBoxButtons.YesNo;

MessageBoxIcon icons = MessageBoxIcon.Information;

// Displays the MessageBox.

DialogResult OpenNotePad;

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

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

if (MessageBox.Show(this,
"Do you want ...", "Clipboard", MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.Yes) {
...

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****@sturmnet.orgwrote in message
news:1n****************@sturmnet.org...
Hi,

You're creating the order in question yourself, by using this iteration:
>foreach (DataGridViewRow row in dgMainView.SelectedRows)

So iterating the other way round should reverse your order:

for (int i = dgMainView.SelectedRows.Count - 1; i >= 0; i--) {
DataGridViewRow row = dgMainView.SelectedRows[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.SetData(DataFormats.Text, sb.ToString());

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

string message = counter + " rows copied to clipboard.\nWould 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";

MessageBoxButtons buttons = MessageBoxButtons.YesNo;

MessageBoxIcon icons = MessageBoxIcon.Information;

// Displays the MessageBox.

DialogResult OpenNotePad;

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

if (OpenNotePad == DialogResult.Yes)

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

if (MessageBox.Show(this,
"Do you want ...", "Clipboard", MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.Yes) {
...

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
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 ...
59
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...
31
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...
19
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:...
8
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...
1
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...
3
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...
11
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.