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

Problems with row selection in radgridview

14 Answers 1204 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave Galligher
Top achievements
Rank 2
Dave Galligher asked on 17 Jan 2011, 09:49 PM
I have a need to allow the user to delete multiple rows in a grid that is unbound based on their selection. The problem is that even though the grid is set with the following attributes:

SelectionMode: FullRowSelect
MultiSelect: True

And the rows appear selected via the control key + the mouse click on the row, the row .IsSelected property does not get set to true, it is always false. However, display wise on the screen the rows selected by the user appear to be selected with the orange highlight for the row.

The code that handles the deletion looks like this, the method that calls this block is via a context menu:

           int rowSelected = radGridView.SelectedRows.Count;

            if (rowSelected > 0)
            {
                int rowCount = radGridView.Rows.Count();
                for (int x = rowCount - 1; x >= 1; x--)
                {
                    if (radGridView.Rows[x].IsSelected)
                        radGridView.Rows[x].Delete();
                }
            }    

If the rows are set programmatically via the radgridview.Rows[x].IsSelected value to true, but of course I don't know what the user is going to select or unselect prior to the code running, so that is not an option when loading of information into the grid. Is there some other event that should be called to set the row when the user clicks on the row header, if so what is the property or event that needs to be handled?

I also tried this on handling the SelectChanged event on the grid. But no matter what happens the row.IsSelected property remains false:

private void radGridView_SelectionChanged(object sender, EventArgs e)
        {
            GridViewRowInfo ri = radGridView.CurrentRow;
            int rowIndex = ri.Index;
            if (rowIndex >= 0)
            {
                bool selectedRowState = radGridView.Rows[rowIndex].IsSelected;
                bool updatedSelection = (!selectedRowState);                                                   // sanity check for value
                radGridView.Rows[rowIndex].IsSelected = updatedSelection;                            // value stays false even after setting to true?
            }
        }



Thank you.

14 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 10:40 AM
Hello,

Please try this sample. It allows you to show the number of selected rows and delete them from an unbound grid.

Designer File
namespace RadGridView_MultipleRowDelete_C
{
    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();
            this.ButtonDelete = new Telerik.WinControls.UI.RadButton();
            this.ButtonSelectedRowCount = new Telerik.WinControls.UI.RadButton();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.ButtonDelete)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.ButtonSelectedRowCount)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(497, 370);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            // 
            // ButtonDelete
            // 
            this.ButtonDelete.Location = new System.Drawing.Point(352, 397);
            this.ButtonDelete.Name = "ButtonDelete";
            this.ButtonDelete.Size = new System.Drawing.Size(130, 24);
            this.ButtonDelete.TabIndex = 1;
            this.ButtonDelete.Text = "Delete Rows";
              
            // 
            // ButtonSelectedRowCount
            // 
            this.ButtonSelectedRowCount.Location = new System.Drawing.Point(205, 397);
            this.ButtonSelectedRowCount.Name = "ButtonSelectedRowCount";
            this.ButtonSelectedRowCount.Size = new System.Drawing.Size(130, 24);
            this.ButtonSelectedRowCount.TabIndex = 2;
            this.ButtonSelectedRowCount.Text = "Selected row count";
              
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(494, 433);
            this.Controls.Add(this.ButtonSelectedRowCount);
            this.Controls.Add(this.ButtonDelete);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
              
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.ButtonDelete)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.ButtonSelectedRowCount)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
        private Telerik.WinControls.UI.RadButton ButtonDelete;
        private Telerik.WinControls.UI.RadButton ButtonSelectedRowCount;
    }
}

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 RadGridView_MultipleRowDelete_C
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.radGridView1.SelectionChanged += new System.EventHandler(this.radGridView1_SelectionChanged);
            this.ButtonSelectedRowCount.Click += new System.EventHandler(this.ButtonSelectedRowCount_Click);
            this.ButtonDelete.Click += new System.EventHandler(this.ButtonDelete_Click);
            this.Load += new System.EventHandler(this.Form1_Load);
        }
  
  
        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.MultiSelect = true;
            this.radGridView1.AllowDeleteRow = true;
        }
  
  
        private void radGridView1_SelectionChanged(object sender, EventArgs e)
        {
            this.ButtonDelete.Enabled = (this.radGridView1.SelectedRows.Count > 0);
        }
  
        private void ButtonDelete_Click(object sender, EventArgs e)
        {
            GridViewRowInfo[] selectedRows = radGridView1.SelectedRows.ToArray();
            for (int i = selectedRows.Length - 1; i >= 0; i += -1)
            {
                GridViewRowInfo row = selectedRows[i];
                radGridView1.Rows.Remove(row);
            }
  
  
        }
  
        private void ButtonSelectedRowCount_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.radGridView1.SelectedRows.Count.ToString());
        }
  
  
    }
}

hope that helps
Richard
0
Dave Galligher
Top achievements
Rank 2
answered on 18 Jan 2011, 04:29 PM
Thank you, but that didn't work.

I appreciate the response, but the problem is the row state does not change to selected. Therefore when this block of code in your example is ran:

GridViewRowInfo[] selectedRows = radGridView.SelectedRows.ToArray();
            for (int i = selectedRows.Length - 1; i >= 0; i += -1)
            {
                GridViewRowInfo row = selectedRows[i];
                radGridView.Rows.Remove(row);
            }

The radGridView.SelectedRows returns an empty array. The same problem I was having. This should be a fairly simple deal, user selects the rows they want to delete and then a short series of code like above deletes them. I can do them one at a time using this method:

 int rowIndex = radGridView.CurrentRow.Index;
            radGridView.Rows[rowIndex].Delete();
            radGridView.TableElement.Update(GridUINotifyAction.StateChanged);

But we need to allow them to select multiples. I double checked the settings on my grid and the AllowDeleteRow is set to true. As stated previously, the primary problem is the row state does not get set to IsSelected=true when the user actually selects the row.
0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 04:32 PM
Hello,

The exmaple I gave is avble to delete multiple rows and return the correct number of rows selected when clicking on the ButtonSelectedRowCount button. Please can you confirm which version you are using?
for reference im on the latest Q3 2010 release.
Thanks
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 04:35 PM
For reference, here is a small video of it working under the latest version.
Please let me know if I have misunderstood your request
Thanks
Richard
0
Dave Galligher
Top achievements
Rank 2
answered on 18 Jan 2011, 04:57 PM

Richard:

Due to other problems that are currently reported I have had to stay on 2012.2.10.806. There are major speed issues with the current build. The grid has over 300 columns in it and slows to a crawl, even with no data in it. I have been trying to get information from Telerik on where to send the project duplicating the problem and no response from so far. Please follow this link to see what I am talking about: http://www.telerik.com/community/forums/wpf/gridview/speed-slowdown-using-sp2010-3-10-1215.aspx

Unfortunately the website does not allow the attachment of zip files to a thread, and since no incident number is given, and no location is given to e-mail anything, it's pretty futile to do anything but wait. I'm going to be contacting them today by phone if I don't hear anything. It is extremely frustrating.

If you can offer suggestions to overcome the issues described above, all help is sincerely appreciated. Thank you.

0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 05:00 PM
Hello,

My advice would be to download a trial version of the latest controls and give it a go. There used to be some issues with lots of columns but as far as I know these are solved. There are also many other enhancements, upgrades, fixes and new controls that you can take advantage of in the latest version.
Regards,
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 05:01 PM
for reference, to send Telerik a project you should open a new support ticket where you can attach zip files. This can be done throug your account section of this website.
regards,
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 05:43 PM
I've noticed that your last post referred to a problem in WPF, not WinForms. Can you confirm you are indeed using WinForms grid?
Thanks
Richard
0
Dave Galligher
Top achievements
Rank 2
answered on 18 Jan 2011, 06:03 PM
I will try your suggestions.
0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 06:16 PM
Hello Dave,

Let me try to answer your questions as best I can.
  • The forum threads only allow pictures to be added. Whereas support tickets allow you to add zip files, and therefore projects.
  • If you've had no response from Telerik then the ticket may be closed. If so, this will be indicated against the ticket in your account section. You can re-open a ticket at any time by sending another reply or response.
  • Support tickets (depending on your licence) are given a 24 hour turnaround time by Telerik wheras they aim to review forum posts within 72 hours
  • I asked about the WPF question as you included a link to a WPF forum post in one of your previous replies.
  • Sadly, I don't work for Telerik. I'm just another forum user like yourself. If you've tried my example and had a look at the video that I posted and have spotted an error with it, please let me know and I'll do my best to help you, But at the moment, I cannot seem to replicate your error.
  • The very fastest way to resolve an issue that cannot be resolved by other means of support is to open a support ticket and send an example project that details the issue, together with the version of the controls you are usibng etc..

Do let me know if I can help further but I would ask that you try the exmaple that I have posted in the latest version if possible. Sadly, I don't have access to previous versions at the moment so I cannot test with your version of the controls.

All the best
Richard

edit// reply corresponds to updated forum post.

0
Dave Galligher
Top achievements
Rank 2
answered on 18 Jan 2011, 06:21 PM
It is a WINFORMS project, and thank you for your assistance. I referenced your help on the other thread. I edited my previous posting. Thank you again for your assistance.
0
Richard Slade
Top achievements
Rank 2
answered on 18 Jan 2011, 06:26 PM
Ok Dave,

If I can help further please let me know
thanks
Richard
0
Sailaja
Top achievements
Rank 1
answered on 27 Feb 2013, 05:04 PM
Hi Dave,

My issue is also same as yours. Can I know the link of the forum where you got the solution.

Appreciate your help.
0
Stefan
Telerik team
answered on 28 Feb 2013, 01:57 PM
Hi Sailaja,

I am copying my answer from your support ticket, so the community can benefit from it.
"...in your code you are using CellSelect SelectionMode and this is the reason, why the SelectedRows collection is empty. In this mode, you should use the SelectedCells collection, from where you can get each cell's RowInfo, save the rows in some kind of collection and delete them..."

Here is a sample code:
Imports System.ComponentModel
Imports Telerik.WinControls.UI
 
Public Class Form1
    Dim OrgFolderList As New BindingList(Of MyObjectClass)
    Dim myContextMenu As New RadContextMenu
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
        For i As Integer = 0 To 9
            Dim ob As New MyObjectClass
            ob.ID = i
            ob.Name = "Name " & i
            OrgFolderList.Add(ob)
        Next
 
        Me.RadGridView1.DataSource = OrgFolderList
 
        RadGridView1.MultiSelect = True
        RadGridView1.SelectionMode = GridViewSelectionMode.CellSelect
        RadGridView1.AllowDeleteRow = True
 
        
    End Sub
 
    Private Sub mitemFileDelete_Click(ByVal s As Object, ByVal e As EventArgs)
 
        Dim selectedRows As New List(Of GridViewDataRowInfo)
        For Each cell As GridViewCellInfo In RadGridView1.SelectedCells
            selectedRows.Add(cell.RowInfo)
        Next
 
        Dim i As Integer = selectedRows.Count - 1
        While i >= 0
            Dim row As GridViewRowInfo = selectedRows(i)
            RadGridView1.Rows.Remove(row)
            i += -1
        End While
    End Sub
End Class
 
 
Class MyObjectClass
    Private _id As Integer
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property
 
    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
End Class

I hope this helps.

All the best,
Stefan
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Join us for a FREE webinar to see all the new stuff in action.

Interested, but can’t attend? Register anyway and we’ll send you the recording.
Tags
GridView
Asked by
Dave Galligher
Top achievements
Rank 2
Answers by
Richard Slade
Top achievements
Rank 2
Dave Galligher
Top achievements
Rank 2
Sailaja
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or