StrataFrame Forum

Is there any way to change the selected row in a DataGridView programmatically?

http://forum.strataframe.net/Topic19123.aspx

By Marcia G Akins - 9/5/2008

Hi All.

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.

TIA

By Edhy Rijo - 9/5/2008

Hi Marcia,

I believe you are using a datagrid to display the data.  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.

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.

By Dustin Taylor - 9/5/2008

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.

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.

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.

By Marcia G Akins - 9/5/2008

Edhy Rijo (09/05/2008)
Hi Marcia,

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.

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 Smile

Thanks for the advice.

By Marcia G Akins - 9/5/2008

Dustin Taylor (09/05/2008)

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.

Color me stupid, but I do not see any RightClick event in the DataGridView that I can handle Unsure

If you can tell me where it is, I would be happy to hook into it Smile

By StrataFrame Team - 9/5/2008

It's probably called MouseClick or PreviewMouseClick.  The event args will tell you which button(s) was clicked to raise the event.
By StrataFrame Team - 9/5/2008

Yep, MouseClick is the general one.  There's also a bunch of Mouse* methods like MouseDown and MouseUp you can handle.  Also, the grid has CellClick and CellDoubleClick, and ones like ColumnHeaderMouseClick and ColumnHeaderMouseDoubleClick.  Almost all of those will have MouseEventArgs which will tell you the mouse button that was used.  Just test on e.Button == MouseButtons.Right or something like that in your handler and you should be in business.
By Marcia G Akins - 9/5/2008

Ben Chase (09/05/2008)
It's probably called MouseClick or PreviewMouseClick.  The event args will tell you which button(s) was clicked to raise the event.

Yes. I found that event. What I couldn't find was a way to set the SelectedRow in the grid.

By Edhy Rijo - 9/5/2008

Marcia G Akins (09/05/2008)
Yes. I found that event. What I couldn't find was a way to set the SelectedRow in the grid.

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. 

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. Tongue

By Marcia G Akins - 9/5/2008

Edhy Rijo (09/05/2008)
Marcia G Akins (09/05/2008)
Yes. I found that event. What I couldn't find was a way to set the SelectedRow in the grid.

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. 

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. Tongue

I used the listview quite a bit in VFP Smile

And after your last reply, I looked at the ListView in Strataframe.

It also had no RightCLick event that I could hook in to. I will re-visite this issue after my meeting with the client Smile

I truely appreciate all of the help you ave given me.

By Bill Cunnien - 9/8/2008

Could you add a context menu to the grid?  I have done this with the DevExpress XtraGrid...works quite well.

Bill

By Edhy Rijo - 9/8/2008

Marcia G Akins (09/05/2008)
And after your last reply, I looked at the ListView in Strataframe.

I suggested you to look at the StrataFlix sample application which uses the new enhanced list.

It also had no RightCLick event that I could hook in to.

A single event, the MouseClick event is used to handle all this in .NET controls like this:

Private Sub lstServiceCalls_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstServiceCalls.MouseClick

     If e.Button = Windows.Forms.MouseButtons.Right Then

          '-- do your stuff here.    

     End If

End Sub

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. 

BTW, StrataFlix Main Form also have a sample of a ThemedContextMenuStrip.