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

Mutiselection problem

10 Answers 75 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lily
Top achievements
Rank 1
Lily asked on 25 Feb 2011, 08:30 PM
I have a grid where I can multiselect rows.
When I select several rows

radGridView1.MasterGridViewTemplate.SelectedRows.Count shows the correct number of selected rows. But if grid is sorted sometemes it shows the correct number, sometimes 1, sometimes 0. I chet in event on context menu click. So sometimes menu  works on correct rows, sometime on one of seleted, sometimes does nothing
Thank you

Lily.

10 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 10:46 PM
Hello Lily,

Hope you're well.

I'm afraid I haven't been able to replicate your issue. I have included a sample below where multiselection is on and the count is provided by a context menu.

Designer File
namespace RadControlsWinFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            // 
            // radGridView1
            // 
            this.radGridView1.MasterTemplate.MultiSelect = true;
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(387, 381);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.ContextMenuOpening += new Telerik.WinControls.UI.ContextMenuOpeningEventHandler(this.radGridView1_ContextMenuOpening);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(387, 381);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
namespace RadControlsWinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));
            this.radGridView1.Rows.Add("A1", "B1");
            this.radGridView1.Rows.Add("A2", "B2");
            this.radGridView1.Rows.Add("A3", "B3");
            this.radGridView1.Rows.Add("A4", "B4");
            this.radGridView1.Rows.Add("A5", "B5");
            this.radGridView1.Rows.Add("A6", "B6");
            this.radGridView1.Rows.Add("A7", "B7");
            this.radGridView1.Rows.Add("A8", "B8");
            this.radGridView1.Rows.Add("A9", "B9");
            this.radGridView1.Rows.Add("A10", "B10");
  
  
  
        }
  
        private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            if (this.radGridView1.CurrentRow.Index > -1)
            {
                e.ContextMenu.Items.Clear();
                RadMenuItem item = new RadMenuItem("Click me for count");
                e.ContextMenu.Items.Add(item);
                item.Click += new EventHandler(item_Click);
            }
        }
  
        void item_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.radGridView1.SelectedRows.Count.ToString() + " selected rows");
        }
    }
}

Please could you review the code and let me know if that helps or if you can reproduce it with this.
thanks
Richard
0
Lily
Top achievements
Rank 1
answered on 25 Feb 2011, 11:41 PM
Your code is working, my does not. Sometimes I got an exception ( screen shot is enclosed)
This is a grid in Designer:


this

 

.radGridView1.AutoScroll = true;

 

 

 

this.radGridView1.AutoSize = true;

 

 

 

this.radGridView1.BackColor = System.Drawing.SystemColors.Control;

 

 

 

this.radGridView1.Cursor = System.Windows.Forms.Cursors.Default;

 

 

 

this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;

 

 

 

this.radGridView1.Font = new System.Drawing.Font("Segoe UI", 8.25F);

 

 

 

this.radGridView1.ForeColor = System.Drawing.Color.Black;

 

 

 

this.radGridView1.ImeMode = System.Windows.Forms.ImeMode.NoControl;

 

 

 

this.radGridView1.Location = new System.Drawing.Point(0, 0);

 

 

 

//

 

 

// radGridView1

 

 

//

 

 

this.radGridView1.MasterTemplate.AllowAddNewRow = false;

 

 

 

this.radGridView1.MasterTemplate.AllowDeleteRow = false;

 

 

 

this.radGridView1.MasterTemplate.AllowEditRow = false;

 

 

 

this.radGridView1.MasterTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;

 

 

gridViewTextBoxColumn1.FieldName =

"[Id]";

 

 

gridViewTextBoxColumn1.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewTextBoxColumn1.HeaderText =

"Id";

 

 

gridViewTextBoxColumn1.IsVisible =

false;

 

 

gridViewTextBoxColumn1.Name =

"colId";

 

 

gridViewTextBoxColumn1.VisibleInColumnChooser =

false;

 

 

gridViewTextBoxColumn1.Width = 42;

 

gridViewTextBoxColumn2.AllowGroup =

false;

 

 

gridViewTextBoxColumn2.FieldName =

"[Name]";

 

 

gridViewTextBoxColumn2.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewTextBoxColumn2.HeaderText =

"Name";

 

 

gridViewTextBoxColumn2.HeaderTextAlignment = System.Drawing.

ContentAlignment.MiddleLeft;

 

 

gridViewTextBoxColumn2.Name =

"colName";

 

 

gridViewTextBoxColumn2.Width = 247;

 

gridViewTextBoxColumn3.FieldName =

"[Trust]";

 

 

gridViewTextBoxColumn3.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewTextBoxColumn3.HeaderText =

"Trust";

 

 

gridViewTextBoxColumn3.HeaderTextAlignment = System.Drawing.

ContentAlignment.MiddleLeft;

 

 

gridViewTextBoxColumn3.Name =

"colTrust";

 

 

gridViewTextBoxColumn3.ReadOnly =

true;

 

 

gridViewTextBoxColumn3.Width = 84;

 

gridViewTextBoxColumn4.FieldName =

"[Approved]";

 

 

gridViewTextBoxColumn4.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewTextBoxColumn4.HeaderText =

"Approved";

 

 

gridViewTextBoxColumn4.Name =

"colApproved";

 

 

gridViewTextBoxColumn4.TextAlignment = System.Drawing.

ContentAlignment.MiddleCenter;

 

 

gridViewTextBoxColumn4.TextImageRelation = System.Windows.Forms.

TextImageRelation.ImageAboveText;

 

 

gridViewTextBoxColumn4.Width = 72;

 

gridViewTextBoxColumn5.FieldName =

"[Local]";

 

 

gridViewTextBoxColumn5.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewTextBoxColumn5.HeaderText =

"Local";

 

 

gridViewTextBoxColumn5.IsVisible =

false;

 

 

gridViewTextBoxColumn5.Name =

"colLocal";

 

 

gridViewTextBoxColumn5.TextAlignment = System.Drawing.

ContentAlignment.MiddleCenter;

 

 

gridViewTextBoxColumn5.VisibleInColumnChooser =

false;

 

 

gridViewTextBoxColumn5.Width = 36;

 

gridViewCheckBoxColumn1.FieldName =

"Acknowledged";

 

 

gridViewCheckBoxColumn1.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewCheckBoxColumn1.HeaderText =

"Reviewed";

 

 

gridViewCheckBoxColumn1.MinWidth = 20;

 

gridViewCheckBoxColumn1.Name =

"colAcknowledged";

 

 

gridViewCheckBoxColumn1.Width = 72;

 

gridViewDateTimeColumn1.AllowGroup =

false;

 

 

gridViewDateTimeColumn1.FieldName =

"[FirstSeenDate]";

 

 

gridViewDateTimeColumn1.Format = System.Windows.Forms.

DateTimePickerFormat.Long;

 

 

gridViewDateTimeColumn1.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewDateTimeColumn1.HeaderText =

"First Seen";

 

 

gridViewDateTimeColumn1.HeaderTextAlignment = System.Drawing.

ContentAlignment.MiddleLeft;

 

 

gridViewDateTimeColumn1.Name =

"colFirstSeen";

 

 

gridViewDateTimeColumn1.Width = 170;

 

gridViewDateTimeColumn2.AllowGroup =

false;

 

 

gridViewDateTimeColumn2.FieldName =

"[LastModifiedDate]";

 

 

gridViewDateTimeColumn2.Format = System.Windows.Forms.

DateTimePickerFormat.Short;

 

 

gridViewDateTimeColumn2.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewDateTimeColumn2.HeaderText =

"Last Modified";

 

 

gridViewDateTimeColumn2.HeaderTextAlignment = System.Drawing.

ContentAlignment.MiddleLeft;

 

 

gridViewDateTimeColumn2.IsVisible =

false;

 

 

gridViewDateTimeColumn2.Name =

"colDate";

 

 

gridViewDateTimeColumn2.Width = 153;

 

gridViewTextBoxColumn6.AllowGroup =

false;

 

 

gridViewTextBoxColumn6.AllowResize =

false;

 

 

gridViewTextBoxColumn6.AllowSort =

false;

 

 

gridViewTextBoxColumn6.FieldName =

"[Acknowledged]";

 

 

gridViewTextBoxColumn6.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewTextBoxColumn6.HeaderText =

"column1";

 

 

gridViewTextBoxColumn6.IsVisible =

false;

 

 

gridViewTextBoxColumn6.Name =

"colAckText";

 

 

gridViewTextBoxColumn6.VisibleInColumnChooser =

false;

 

 

gridViewTextBoxColumn6.Width = 47;

 

gridViewTextBoxColumn7.AllowGroup =

false;

 

 

gridViewTextBoxColumn7.AllowResize =

false;

 

 

gridViewTextBoxColumn7.AllowSort =

false;

 

 

gridViewTextBoxColumn7.FieldName =

"[gsr]";

 

 

gridViewTextBoxColumn7.FormatInfo =

new System.Globalization.CultureInfo("");

 

 

gridViewTextBoxColumn7.HeaderText =

"GSR";

 

 

gridViewTextBoxColumn7.HeaderTextAlignment = System.Drawing.

ContentAlignment.MiddleLeft;

 

 

gridViewTextBoxColumn7.IsVisible =

false;

 

 

gridViewTextBoxColumn7.Name =

"colGSR";

 

 

gridViewTextBoxColumn7.VisibleInColumnChooser =

false;

 

 

gridViewTextBoxColumn7.Width = 48;

 

 

this.radGridView1.MasterTemplate.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] {

 

 

gridViewTextBoxColumn1,

 

gridViewTextBoxColumn2,

 

gridViewTextBoxColumn3,

 

gridViewTextBoxColumn4,

 

gridViewTextBoxColumn5,

 

gridViewCheckBoxColumn1,

 

gridViewDateTimeColumn1,

 

gridViewDateTimeColumn2,

 

gridViewTextBoxColumn6,

 

gridViewTextBoxColumn7});

 

 

this.radGridView1.MasterTemplate.DataSource = this.trpubsGUIBindingSource;

 

 

 

this.radGridView1.MasterTemplate.EnableFiltering = true;

 

 

 

this.radGridView1.MasterTemplate.MultiSelect = true;

 

 

 

this.radGridView1.MasterTemplate.ShowFilteringRow = false;

 

 

 

this.radGridView1.MasterTemplate.ShowGroupedColumns = true;

 

 

 

this.radGridView1.Name = "radGridView1";

 

 

 

this.radGridView1.Padding = new System.Windows.Forms.Padding(0, 0, 0, 1);

 

 

 

this.radGridView1.ReadOnly = true;

 

 

 

this.radGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No;

 

 

 

//

 

 

//

 

 

//

 

 

this.radGridView1.RootElement.ForeColor = System.Drawing.Color.Black;

 

 

 

this.radGridView1.RootElement.Padding = new System.Windows.Forms.Padding(0, 0, 0, 1);

 

 

 

this.radGridView1.ShowGroupPanel = false;

 

 

 

this.radGridView1.Size = new System.Drawing.Size(663, 185);

 

 

 

this.radGridView1.TabIndex = 3;

 

 

 

this.radGridView1.Text = "radGridView1";

 

 

 

this.radGridView1.CurrentRowChanged += new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.radGridView1_CurrentRowChanged);

 

 

 

this.radGridView1.ContextMenuOpening += new Telerik.WinControls.UI.ContextMenuOpeningEventHandler(this.radGridView1_ContextMenuOpening);

 

 

 

this.radGridView1.CellClick += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellClick);

 

 

 

this.radGridView1.GroupSummaryEvaluate += new Telerik.WinControls.UI.GroupSummaryEvaluateEventHandler(this.radGridView1_GroupSummaryEvaluate);

 

 

 

this.radGridView1.GroupByChanged += new Telerik.WinControls.UI.GridViewCollectionChangedEventHandler(this.radGridView1_GroupByChanged);

 

 

 

this.radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(this.radGridView1_CellFormatting);

 

 

 

this.radGridView1.ViewRowFormatting += new Telerik.WinControls.UI.RowFormattingEventHandler(this.radGridView1_ViewRowFormatting);

 

 

 

this.radGridView1.SelectionChanged += new System.EventHandler(this.radGridView1_SelectionChanged);

 

 

 

this.radGridView1.Click += new System.EventHandler(this.radGridView1_Click);

My grid has datasource. Exception happened several times, but it is not stable.
Thank you
Lily.

 

0
Richard Slade
Top achievements
Rank 2
answered on 26 Feb 2011, 12:12 AM
Hi Lily,

I still haven't been able to replicate this though I see that the exception references clearing selected rows. Are you able to paste in here (using the format code block to correctly format C#) both the designer file and form for me to try.
Thanks
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Feb 2011, 12:57 PM
Hello Lily, Richard,

Lilly, Richard is right, please try to use the code format code block to post code otherwise it is unreadable.
Also i have seen that you have a lot of eventhandlers added, selection changed, are you sure you are not clearing the selection in one of those?

Until then can you please try the following?
void button_Click(object sender, EventArgs e)
{
    var selectedRowsCount = radGridView1.CurrentView.ViewInfo.Rows.Count(r => r.IsSelected);
    var selectedRows = radGridView1.SelectedRows.Count;
    MessageBox.Show("SelectedRowsCounted: " + selectedRowsCount + ", SelectedRows:" + selectedRows);
}

on a button click anywhere. Because the CurrentView should always have the latest info in the ViewInfo.Rows about the current state of the rows, this should tell us if the problem is coming from the SelectedRows property or from somewhere else.

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

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Lily
Top achievements
Rank 1
answered on 02 Mar 2011, 08:43 PM
Your code is vot compiling:
Error 1 Non-invocable member 'Telerik.WinControls.UI.GridViewChildRowCollection.Count' cannot be used like a method. 
0
Richard Slade
Top achievements
Rank 2
answered on 02 Mar 2011, 09:40 PM
Hi Lily,

I suspect that you are using .NET Framework 2.0, and therefore it is failing on this line
var selectedRowsCount = radGridView1.CurrentView.ViewInfo.Rows.Count(r => r.IsSelected);
which is fine wen using a later version of the .NET Framework. It is failing (if this is the case) because Count is only a property in .NET 2.0 and does not accept linq queries.
Richard
0
Lily
Top achievements
Rank 1
answered on 02 Mar 2011, 09:55 PM
Yes, we are using 2.0
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 Mar 2011, 05:48 AM
Hello again,

If you are using 2.0 you can just iterate over the radGridView1.CurrentView.ViewInfo.Rows, and foreach selected row increment a counter or do something else. It should work like this.

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

Best Regards,
Emanuel Varga
0
Vivek
Top achievements
Rank 1
answered on 03 Mar 2011, 09:58 PM
Emanuel,

Is this a known issue with using Telerik controls with Dot Net 2.0 that multi select does not always return the correct number of selected rows. 

Thanks,
Vivek
0
Svett
Telerik team
answered on 09 Mar 2011, 08:47 AM
Hello guys,

I would kindly ask you to open a support ticket where you have enclosed your projects. In addition, could you explain the exact steps to reproduce it? This will help us to understand the exact scenario in depth for each of you.

Kind regards,
Svett
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
Lily
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Lily
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Vivek
Top achievements
Rank 1
Svett
Telerik team
Share this question
or