Hello,
I am trying to figure out on how to get the selected row and column index on a RadGridView in a MouseDown event. What I am trying to do is when the user right clicks, a context menu strip pops up with options to do something..
P.S. I am trying to do it in C# VS2010.
Thank you,
Greatly Appreciated.
10 Answers, 1 is accepted
This should do what you need. It's just a RadGridView on a form.
Imports Telerik.WinControls Imports Telerik.WinControls.UI Imports System.Globalization Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim list As New List(Of person) list.Add(New person("Richard", 1)) list.Add(New person("Bob", 2)) list.Add(New person("Stewart", 3)) list.Add(New person("Chris", 4)) list.Add(New person("Leisl", 1)) list.Add(New person("Tom", 5)) list.Add(New person("Oly", 6)) Me.RadGridView1.DataSource = list End Sub Private Sub RadGridView1_ContextMenuOpening(ByVal sender As System.Object, _ ByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles RadGridView1.ContextMenuOpening e.ContextMenu.Items.Clear() Dim menuItem As New RadMenuItem("My Item") AddHandler menuItem.Click, AddressOf MenuItem_Click e.ContextMenu.Items.Add(menuItem) End Sub Private Sub MenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) MessageBox.Show("Column: " & Me.RadGridView1.CurrentColumn.Index.ToString() & " Row:" & Me.RadGridView1.CurrentRow.Index.ToString()) End Sub End Class Public Class person Private m_Id As Integer Private m_Name As String Public Sub New() End Sub Public Sub New(ByVal name As String, ByVal id As Integer) m_Name = name m_Id = id End Sub Public Property Name() As String Get Return m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Public Property Id() As Integer Get Return m_Id End Get Set(ByVal value As Integer) m_Id = value End Set End PropertyEnd ClassLet me know if you need more help
Richard
Hello Richard,
I really appreciate your help, but I am not sure I understands the code you've posted. Sorry, forgive me. My programming experiences are limited and also programming languages. I am trying to do it in C#, I am sure VB is simliar..but I would be great if you have a simple C# example would be great.
Thank you so much...
Please try the following example:
using System;using System.ComponentModel;using System.Windows.Forms;using Telerik.WinControls.UI;public partial class Form1 : Form{ private RadGridView radGridView1; public Form1() { InitializeComponent(); this.Controls.Add(radGridView1 = new RadGridView()); radGridView1.Dock = DockStyle.Fill; radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; radGridView1.ContextMenuOpening += new ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); radGridView1.DataSource = new BuyersCollection(); } void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e) { if (e.ContextMenuProvider is GridDataCellElement) { // this context menu has been fired from the cell e.ContextMenu.Items.Clear(); var menuItem = new RadMenuItem("MenuItem1"); menuItem.Click += new EventHandler(menuItem_Click); var menuItem2 = new RadMenuItem("MenuItem2"); menuItem2.Click += new EventHandler(menuItem2_Click); e.ContextMenu.Items.AddRange(menuItem, menuItem2); } } void menuItem2_Click(object sender, EventArgs e) { var currentCell = radGridView1.CurrentCell; var currentRow = radGridView1.CurrentRow; MessageBox.Show("Menu Item 2 clicked"); } void menuItem_Click(object sender, EventArgs e) { var currentCell = radGridView1.CurrentCell; var currentRow = radGridView1.CurrentRow; MessageBox.Show("Menu Item 1 clicked"); }}public class Buyer{ public int Id { get; set; } public string Name { get; set; } public Buyer(int id, string name) { this.Id = id; this.Name = name; }}public class BuyersCollection : BindingList<Buyer>{ public BuyersCollection() { for (int i = 0; i < 10; i++) { this.Add(new Buyer(i, "Buyer" + (i))); } }}If i understood correctly you only need to show your custom menu when rightclicking on cells, not on the header also, right?
And for the clicked cell and row, it will always be the current cell and current row from the grid, so you can just get them from the grid with radGridView1.CurrentCell or radGridView1.CurrrentRow
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
The example I provided, and the one Emanuel provided in C# will work for you. To give you an explanation of what is happening, is that the ContextMenuOpening event has been registered for the grid. This is because the grid already has a context menu associated with it. The code is clearing the items in that context menu and adding it's own custom one. A click event is then being registered for that menu item.
When a user right clicks and gets the context menu it is in context of a row and cell. Therefore in the click event of the context menu you can display the current row / current cell which are directly available from the RadGridView object.
For future reference, there is also a code converter that you may find useful at this link
Hope this helps but let me know if you need more information
Richard
Hello Emanuel Varga and Richard Slade,
Thank you so much for the examples and the explanation. You guys are the BEST! But I have a confusion as in.. I created my RadGridView via UI and then I bind the data to it from a datatable. Everything works for my RadGridView, all I want is to have a context menu to pop up when user right click to see different options. The example from Emanuel works great and it's a good example to learn from. But how can use part of his coded and tie it to my RadGridView that I created from the UI to have a pop up context menu? I didnt' know that the RadGridView has its own context menu. I created a context menu manually and then capture the right click event....but that's where I am stuck at...I want to co capture the selected row and column index when user right clicks. But I don't know how to capture the GridViewCellEventArgs from a MouseDown event...
Either the context menu of the RadGridView or the RadContextMenu control works for me. But either way, I am still stuck.. :(
Thank you.
Anh
You don't have to get those, when you click / right click on a cell, that cell becomes the selected cell so you can just use:
var currentCell = radGridView1.CurrentCell;var currentRow = radGridView1.CurrentRow;Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
Emanuel Varga,
You are the greatest!! That does it...Thank you so much. But the example above is good for learning too.
P.S. what's the code to have the RadGridView horizontal scroll to appears to scroll left and right, if the columns width is resize or more columns are added from the column choosers. My problem is that when I resize the width of the column..the horizontal scroll bar doesn't appear. Instead..the other 3 columns width gets smaller....
Thanks again.!
For the horizontal scroll bar to appear, you have to set the AutoSizeColumnsMode to None, like so:
radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;Because if you have it set to Fill, the grid will try to use just the available space for the columns, so basically it will resize other columns in order to make room for the newly added columns.
Basically if you want horizontal scrollbars you will have to manage the cell width's.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
That does it again...Thank you so much for your help. I would never figure that out without the help I get here.
Thanks again...
Glad to be able to help,
Just remember the mark as answer function is there so you can mark all of the answers that helped you solve your question or questions.
If you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP