This is a migrated thread and some comments may be shown as answers.

MultiSelect and menu

4 Answers 154 Views
GridView
This is a migrated thread and some comments may be shown as answers.
bruno
Top achievements
Rank 1
bruno asked on 11 Oct 2010, 05:21 PM
Hi.

I have found an issue in RadGridView when using multiselect and menu.
What i want to do is: select multiple lines, right click on RadGridView and execute some action on each line.
Apparently no big deal, done it with this code:

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private RadMenuItem mnuAdd = new RadMenuItem("Add");
 
 
        private void radGridView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
        {
            e.ContextMenu.Items.Add(mnuAdd);
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            mnuAdd.Click += new EventHandler(mnuAdd_Click);
 
            radGridView1.Rows.AddNew().Tag = false;
            radGridView1.Rows.AddNew().Tag = false;
        }
 
        void mnuAdd_Click(object sender, EventArgs e)
        {
            foreach (GridViewRowInfo row in radGridView1.SelectedRows)
            {
                row.Tag = !((bool)row.Tag);
                row.Cells[0].Value = row.Tag;
            }
        }
    }
}

I only drag and drop a RadGridView control to my form,  changed MultiSelect property to true and added a column.
Now, when i select multi lines (or just one line) i right click and the menu show up with my menu (and others... but also no big deal). If i chose my menu, the row value change from false to true and from true for false. Simple.


The real problem is when i order the column. After that, if i select more than one row and i select my menu, it is thrown this exception:
"InvalidOperationException: Collection was modified; enumeration operation may not execute."

According my visual studio the error it is in the foreach iteration, more exactly in the "in":
foreach (GridViewRowInfo row in radGridView1.SelectedRows)


Ok... its a foreach problem. I try replace the foreach with a for statement:

for (int a = 0; a < radGridView1.SelectedRows.Count; a++)
{
    radGridView1.SelectedRows[a].Tag = !((bool)radGridView1.SelectedRows[a].Tag);
    radGridView1.SelectedRows[a].Cells[0].Value = radGridView1.SelectedRows[a].Tag;
}


With this solution, after i select both rows and right click and select my menu, i found out that radGridView1.SelectedRows only have one row, the one where i right click and is the only one that got changed.

Think this is one bug in RadGridView (or i am doing someting really wrong...).

I would like some help/answer.

Kind Regards,

Bruno Almeida

4 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 11 Oct 2010, 08:00 PM
Hello Bruno,

Please try this:
foreach (GridViewRowInfo row in radGridView1.SelectedRows.ToArray())
            {
                row.Tag = !((bool)row.Tag);
                row.Cells[0].Value = row.Tag;
            }

And you need to do this because: when you have a sort condition applied, every time you change a value the grid automatically performs a sort operation, and when sorting, in your case it is changing the order of the rows inside the SelectedRows property, so when performing a search you would end up changing the value of the same row twice. To avoid this you could easily create an array and set the values there.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
bruno
Top achievements
Rank 1
answered on 13 Oct 2010, 10:08 AM
Hello Emanuel,

It helped me. Thank you a lot.

EDIT/PS: But think it still is a bug, because it happens even if i don't change values and/or don't add/delete rows.

Kind Regards,
Bruno Almeida
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 10:19 AM
Hello Bruno,

Do you still have a problem with this or did has this solved your problem? If there is still something, please describe it again, because i've followed all of the steps you said in your original post, and that solved it...

Please let me know,

Best Regards,
Emanuel Varga
0
bruno
Top achievements
Rank 1
answered on 13 Oct 2010, 10:27 AM
Hello Emanuel,

No, as you told me, i don't have more problems.
That comment was just a note.

Thank you again.

Bruno Almeida
Tags
GridView
Asked by
bruno
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
bruno
Top achievements
Rank 1
Share this question
or