467,147 Members | 1,241 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,147 developers. It's quick & easy.

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
  • viewed: 3832
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Raymond Hettinger | last post: by
59 posts views Thread by Raymond Hettinger | last post: by
31 posts views Thread by Raymond Hettinger | last post: by
19 posts views Thread by John Keeling | last post: by
8 posts views Thread by Bill | last post: by
1 post views Thread by | last post: by
3 posts views Thread by thrill5 | last post: by
11 posts views Thread by Noah | last post: by
1 post views Thread by Doug | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.