reverse the order of selected items

D

Doug

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
 
O

Oliver Sturm [MVP C#]

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;
....

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
 
D

Doug

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 said:
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;
....

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
 

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