auto-highlight row in datagrid

jonathen

Member
Joined
May 12, 2005
Messages
17
Programming Experience
1-3
Hi Guys,

I need help again! I cannot seem to get my datagrid to do what I want. When I run my form, the first row is selected (that's fine) but the first cell is selected with a cursor in it (not fine) whereas I'd rather either no cell or row is selected, or the first row is highlighted (no cursor).

When I click on another row, the row is fully selected which is what I want, but I can't get it to auto-highlight the first row.

I have attached 2 screenshots - 1.gif is the default view when running the form, with the cursor on the cell. 2.gif is the highlighted row when clicked on it. I want to have that automatically when running the form.

Hope this makes sense - any help appreciated. Thanks,

Jon
 

Attachments

  • 1.gif
    1.gif
    2.9 KB · Views: 903
  • 2.gif
    2.gif
    2.9 KB · Views: 892
VB.NET:
Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] onFormLoad([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load

[/size][size=2]DataGrid.Select(1)  'second row will be selected

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

Cheers :)
 
I think you misunderstand - I don't want to select the second row - I have already got it to select the first row. What I want is for what happens in 1.gif to not happen - when I load the form, the first cell is selected in edit mode with the cursor flashing within that cell. I want the first ENTIRE row selected rather than just one cell (in the same way that 2.gif shows a row selected).
 
Ok ... take a look at the attached project and let me know whether you need that or something else ...


cheers :)


edit: i have used an old project (i've adapted for your needs) with name that doesn't refer to this subject but i hope you don't mind it ... ;)
 

Attachments

  • SelectedRow.zip
    35.2 KB · Views: 222
Last edited:
Thanks for that. It appears that in your example, the first entire row is selected when you fire the load button.

However, my datagrid loads data automatically. Upon lanching the exe, what you see in 3.gif (attached) is how the display looks. See how the first row is selected, using DataGrid1.Select(DataGrid1.CurrentRowIndex), but the first cell has the edit cursor in it, even though it's read-only.

What I want to do is make the whole row selected - and any subsequent rows I click on will be wholly selected, without any cell being in "edit" mode.

Does that make sense? Is it possible?

Thanks,

Jon
 

Attachments

  • 3.gif
    3.gif
    1.9 KB · Views: 892
Ok i got you ...
Select row when click on a cell ;)
There is not a direct way to set the selection mode. :(
Moreover DataGrid has a "protected" GridVScrolled member that can be used to scroll the grid but anyway you can derive it from DataGrid and add a ScrollToRow method making own component with certain feature/s. (let me know if you want to try something like that)
Meanwhile we can use some tricks by calling the DataGrid.Select method in the MouseUp event handler.

take a look at the attached project ... kind regards ;)


 

Attachments

  • SelectRowWhenClickOnACell .zip
    36.4 KB · Views: 228
I think you'll find that the selected cell in a GridView will always contain the cursor when the GridView is focused. This is an educated guess, as I haven't tested it, but I'd say that what you need to do is make sure the GridView isn't focused when the Form loads.
 
If someone doesn't want to see the attacment of my previous reply this is the code i've used to achieve the "Select row when click on a cell" ;)


VB.NET:
Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] myGrid_MouseUp([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.MouseEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] myGrid.MouseUp
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] point [/size][size=2][color=#0000ff]As[/color][/size][size=2] Point = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Point(e.X, e.Y)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] hitTest [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataGrid.HitTestInfo = myGrid.HitTest(point)

[/size][size=2][color=#0000ff]If[/color][/size][size=2] hitTest.Type = DataGrid.HitTestType.Cell [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]myGrid.CurrentCell = [/size][size=2][color=#0000ff]New[/color][/size][size=2] DataGridCell(hitTest.Row, hitTest.Column)

myGrid.Select(hitTest.Row)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

Cheers :)
 
Thanks Kulrom - I have been downloading your code - just not straight away as I'm not at my PC all the time.

Your code does exactly the same as:

VB.NET:
Private Sub DataGrid1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
		DataGrid1.Select(DataGrid1.CurrentRowIndex)
End Sub

...inasmuch as when you click a column it selects the entire row - that has always been fine. It's just that when the app first loads, the first cell of the first row is selected in edit mode - once I click another row, the whole row is then selected.
 
This is the entire code (minus the auto generated code)...

VB.NET:
Public Class Form1
	Inherits System.Windows.Forms.Form

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		SqlDataAdapter1.Fill(DataSet11)
		DataGrid1.RowHeadersVisible = False
		DataGrid1.RowHeaderWidth = 0
		DataGrid1.Select(DataGrid1.CurrentRowIndex)

	End Sub

 Private Sub DataGrid1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
		DataGrid1.Select(DataGrid1.CurrentRowIndex)

	End Sub

    Private Sub DataGrid1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DoubleClick
	 MsgBox("You selected Changed Object # " & CType(DataSet11.Tables(0).Rows(DataGrid1.CurrentRowIndex).Item("co_id"), String))
	End Sub

End Class
 
nah ... i tried my best :(

you could add handler right after load event but the issue is still there ... if you try to scrool down/up the grid it shows active cell ...

VB.NET:
myGrid.Select(0) 
 
[/size][size=2][color=#0000ff]AddHandler[/color][/size][size=2] myGrid.MouseUp, [/size][size=2][color=#0000ff]AddressOf[/color][/size][size=2] myGrid_MouseUp

VB.NET:
Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] myGrid_MouseUp([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.MouseEventArgs)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] point [/size][size=2][color=#0000ff]As[/color][/size][size=2] Point = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Point(e.X, e.Y)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] hitTest [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataGrid.HitTestInfo = myGrid.HitTest(point)
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] hitTest.Type = DataGrid.HitTestType.Cell [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]myGrid.CurrentCell = [/size][size=2][color=#0000ff]New[/color][/size][size=2] DataGridCell(hitTest.Row, hitTest.Column)
 
myGrid.Select(hitTest.Row)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub

hmm ... i think that if you insist on this feature you should inherit and extend this functionality of datagrid control ... actually "GridVScrolled" member. Create the component ... would be best for you ...

Cheers :)
 
Thanks, kulrom, for all your help - at least it's not just me missing something obvious. How do I rate you in this forum?

Cheers,

Jon
 
jonathen said:
Thanks, kulrom, for all your help - at least it's not just me missing something obvious. How do I rate you in this forum?

Cheers,

Jon

welcome Jonathen, I'm glad if I helped in some way ... ;)

about "rate me" ... it's only joke (Although there are many forums that provide this option) ... or yet better if someday admin add this feature to the forum i'll be prepared hahaha ... Cheers :)
 
Back
Top