﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » Is there any way to change the selected row in a DataGridView programmatically?</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Tue, 09 Jun 2026 01:52:28 GMT</lastBuildDate><ttl>20</ttl><item><title>Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19123.aspx</link><description>Hi All.&lt;/P&gt;&lt;P&gt;The reason that I want to do this is necause I am using a context menu to allow them to add, edit and delete item (it calls up a modal dialog). It is a little confusing to them if they right click, select edit or delete, and the information from a different row shows up in the modal dialog.&lt;/P&gt;&lt;P&gt;TIA</description><pubDate>Mon, 08 Sep 2008 10:05:58 GMT</pubDate><dc:creator>Marcia G Akins</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19164.aspx</link><description>[quote][b]Marcia G Akins (09/05/2008)[/b][hr]And after your last reply, I looked at the ListView in Strataframe.[/quote] &lt;P&gt;I suggested you to look at the StrataFlix sample application which uses the new enhanced list.&lt;/P&gt;&lt;P&gt;[quote]It also had no RightCLick event that I could hook in to.[/quote]&lt;/P&gt;&lt;P&gt;A single event, the MouseClick event is used to handle all this in .NET controls like this:&lt;/P&gt;&lt;P&gt;[codesnippet]&lt;FONT size=2&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Private&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Sub&lt;/FONT&gt;&lt;FONT size=2&gt; lstServiceCalls_MouseClick(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;ByVal&lt;/FONT&gt;&lt;FONT size=2&gt; sender &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;As&lt;/FONT&gt;&lt;FONT size=2&gt; System.Object, &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;ByVal&lt;/FONT&gt;&lt;FONT size=2&gt; e &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;As&lt;/FONT&gt;&lt;FONT size=2&gt; System.Windows.Forms.MouseEventArgs) &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Handles&lt;/FONT&gt;&lt;FONT size=2&gt; lstServiceCalls.MouseClick&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If&lt;/FONT&gt;&lt;FONT size=2&gt; e.Button = Windows.Forms.MouseButtons.Right &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Then&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;'-- do your stuff here.&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT size=2&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;If&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;End&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Sub&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;[/codesnippet]&lt;/P&gt;&lt;P&gt;And if all you want is to show a shortcut menu (ContextMenuStrip in .NET) all you need to do is add a SF ThemedContextMenuStrip to your form, add all the items you want then add that menu to the form's property ContextMenuStrip and you are done, not need to manage it via the right click like in VFP.&amp;nbsp; &lt;/P&gt;&lt;P&gt;BTW, StrataFlix Main Form also have a sample of a ThemedContextMenuStrip.</description><pubDate>Mon, 08 Sep 2008 10:05:58 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19163.aspx</link><description>Could you add a context menu to the grid?&amp;nbsp; I have done this with the DevExpress XtraGrid...works quite well.&lt;/P&gt;&lt;P&gt;Bill</description><pubDate>Mon, 08 Sep 2008 09:41:05 GMT</pubDate><dc:creator>Bill Cunnien</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19148.aspx</link><description>[quote][b]Edhy Rijo (09/05/2008)[/b][hr][quote][b]Marcia G Akins (09/05/2008)[/b][hr]Yes. I found that event. What I couldn't find was a way to set the SelectedRow in the grid.[/quote] &lt;P&gt;The time and effort you are putting on the grid is good for learning, but taking a 10 minute look at the listview in StrataFlix will give you all the things you are trying to do manually and much more.&amp;nbsp; &lt;P&gt;Trust me I've been there, and did the same thing until I understood that the SF listview is not like the one in VFP which I hardly used. :P[/quote]&lt;P&gt;I used the listview quite a bit in VFP :)&lt;P&gt;And after your last reply, I looked at the ListView in Strataframe.&lt;P&gt;It also had no RightCLick event that I could hook in to. I will re-visite this issue after my meeting with the client :)&lt;P&gt;I truely appreciate all of the help you ave given me.</description><pubDate>Fri, 05 Sep 2008 23:28:21 GMT</pubDate><dc:creator>Marcia G Akins</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19147.aspx</link><description>[quote][b]Marcia G Akins (09/05/2008)[/b][hr]Yes. I found that event. What I couldn't find was a way to set the SelectedRow in the grid.[/quote] &lt;P&gt;The time and effort you are putting on the grid is good for learning, but taking a 10 minute look at the listview in StrataFlix will give you all the things you are trying to do manually and much more.&amp;nbsp; &lt;P&gt;Trust me I've been there, and did the same thing until I understood that the SF listview is not like the one in VFP which I hardly used. :P</description><pubDate>Fri, 05 Sep 2008 20:10:01 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19145.aspx</link><description>[quote][b]Ben Chase (09/05/2008)[/b][hr]It's probably called MouseClick or PreviewMouseClick.&amp;nbsp; The event args will tell you which button(s) was clicked to raise the event.[/quote]&lt;/P&gt;&lt;P&gt;Yes. I found that event. What I couldn't find was a way to set the SelectedRow in the grid.</description><pubDate>Fri, 05 Sep 2008 18:11:32 GMT</pubDate><dc:creator>Marcia G Akins</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19141.aspx</link><description>Yep, MouseClick is the general one.&amp;nbsp; There's also a bunch of Mouse* methods like MouseDown and MouseUp you can handle.&amp;nbsp; Also, the grid has CellClick and CellDoubleClick, and ones like ColumnHeaderMouseClick and ColumnHeaderMouseDoubleClick.&amp;nbsp; Almost all of those will have MouseEventArgs which will tell you the mouse button that was used.&amp;nbsp; Just test on e.Button == MouseButtons.Right or something like that in your handler and you should be in business.</description><pubDate>Fri, 05 Sep 2008 17:10:45 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19140.aspx</link><description>It's probably called MouseClick or PreviewMouseClick.&amp;nbsp; The event args will tell you which button(s) was clicked to raise the event.</description><pubDate>Fri, 05 Sep 2008 17:08:25 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19138.aspx</link><description>[quote][b]Dustin Taylor (09/05/2008)[/b][hr]&lt;P&gt;So for your grid, on a right click you could do a SeekToPrimaryKey(primaryKeyOfClickedRow) to move the CurrentRowIndex of your BO before doing the edit or delete.[/quote]&lt;P&gt;Color me stupid, but I do not see any RightClick event in the DataGridView that I can handle :unsure:&lt;P&gt;If you can tell me where it is, I would be happy to hook into it :)</description><pubDate>Fri, 05 Sep 2008 17:03:02 GMT</pubDate><dc:creator>Marcia G Akins</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19137.aspx</link><description>[quote][b]Edhy Rijo (09/05/2008)[/b][hr]Hi Marcia,&lt;P&gt;When I first started with SF and coming from VFP I tried to use a datagrid, but if the data in the grid/listview will be readonly, then nothing beats the listview enhancements in version 1.6.6.[/quote]&lt;P&gt;Since I practically have this working the way I want, I will take that advice in the future and refactor this form after I have enoough cranked out to show the client at our next meeting :)&lt;P&gt;Thanks for the advice.</description><pubDate>Fri, 05 Sep 2008 17:01:26 GMT</pubDate><dc:creator>Marcia G Akins</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19125.aspx</link><description>Yep, 100% agree with Edhy on this one. The listview can't be beat in most situations. There are still those areas where you need a grid, but you'd be surprised how robust a listview can be while still keeping the interface and design fairly simple. &lt;P&gt;To answer your question, however, you can use the Navigate, Move, or Seek methods of the business object to change the CurrentRowIndex of the business object. Navigate will update any bound controls to reflect the new CurrentRowIndex, while Move and Seek move the CurrentRowIndex pointer without updating any bound controls.&lt;P&gt;So for your grid, on a right click you could do a SeekToPrimaryKey(primaryKeyOfClickedRow) to move the CurrentRowIndex of your BO before doing the edit or delete.</description><pubDate>Fri, 05 Sep 2008 16:14:38 GMT</pubDate><dc:creator>Dustin Taylor</dc:creator></item><item><title>RE: Is there any way to change the selected row in a DataGridView programmatically?</title><link>http://forum.strataframe.net/FindPost19124.aspx</link><description>Hi Marcia,&lt;/P&gt;&lt;P&gt;I believe you are using a datagrid to display the data.&amp;nbsp; If you don't need to modify the data in the grid and will use a modal form, I strongly suggest you use a ListView, this control have been greatly enhanced to support this type of presentation to the end user and it is pretty easy to setup, the StrataFlix Movie form (I believe that is the name) make use of this so you can examine and start using right away.&lt;/P&gt;&lt;P&gt;When I first started with SF and coming from VFP I tried to use a datagrid, but if the data in the grid/listview will be readonly, then nothing beats the listview enhancements in version 1.6.6.</description><pubDate>Fri, 05 Sep 2008 16:05:25 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item></channel></rss>