Selecting row from DataGrid

T

tshad

I have a dataGrid that I filled with files from my directory. I am using VS
2003 and Windows Forms.

************************************************************
Dim dirInfo As DirectoryInfo = New
DirectoryInfo("C:/ImportFile/Coreland")

' FileListTable.Columns.Add("FullName", GetType(String))
FileListTable.Columns.Add("FileName", GetType(String))
FileListTable.Columns.Add("Length", GetType(Integer))
FileListTable.Columns.Add("DateModified", GetType(DateTime))

For Each file As FileInfo In dirInfo.GetFiles("*")
Dim row As DataRow = FileListTable.NewRow()
' row.Item("FullName") = file.FullName
row.Item("FileName") = file.Name
row.Item("Length") = file.Length
row.Item("DateModified") = file.LastAccessTime
FileListTable.Rows.Add(row)
Next

DataGrid1.DataSource = FileListTable
*******************************************

I want to allow the user to select the row and display file in a text box.
I can do this in asp.net but I can't seem to figure out how to do this as an
event so that when the row is selected it automatically goes and gets the
file and displays it.

Maybe by adding a radio button or something. But I am not sure how add that
to the DataGrid and then have it trigger an event.

At the moment I have my tableStyle set as:

***********************************************
Dim gtStyle As New DataGridTableStyle
gtStyle.MappingName = "FileListTable"
gtStyle.AlternatingBackColor = Color.LightBlue

'
' Create GridColumnStyle objects for the grid columns
'
Dim colStyle1 As New DataGridTextBoxColumn
Dim colStyle2 As New DataGridTextBoxColumn
Dim colStyle3 As New DataGridTextBoxColumn

'
' Set column 1's caption, width and disable editing.
'
With colStyle1
.MappingName = "FileName"
.HeaderText = "File name"
.Width = 300
.Alignment = HorizontalAlignment.Left
.TextBox.Enabled = False
End With

'
' Set column 2's caption, width and disable editing.
'
With colStyle2
.MappingName = "Length"
.HeaderText = "Length"
.Width = 100
.Alignment = HorizontalAlignment.Left
.TextBox.Enabled = True
End With

'
' Set column 3's caption, width and disable editing.
'
With colStyle3
.MappingName = "DateModified"
.HeaderText = "Date Modified"
.Width = 100
.Alignment = HorizontalAlignment.Left
.TextBox.Enabled = True
.Format = "yyyy-MM-dd"
End With


' Add the GridColumnStyles to the DataGrid's Column Styles collection.
'
With gtStyle.GridColumnStyles
.Add(colStyle1)
.Add(colStyle2)
.Add(colStyle3)
End With

'
' Add the GridTableStyle to the DataGrid
'
DataGrid1.TableStyles.Add(gtStyle)
***********************************************

Thanks,

Tom
 
M

Miro

Im using vb.2005 express...not sure if it would be the same or if this is right but
this is what I do.

iIntegerValue = CInt(DirectCast(Me.StaffDataGridView.CurrentRow.DataBoundItem, DataRowView).Row.Item("StaffID"))

In my case I just wanted to store the key into an integer value from the Staff Table, from
the StaffID column from the record.

But I think your looking for this:
Me.StaffDataGridView.CurrentRow.DataBoundItem, DataRowView).Row.Item("StaffID")

Hope this helps...

im a newbie so it may not be the right way.

M.
 
T

tshad

Miro said:
Im using vb.2005 express...not sure if it would be the same or if this is
right but
this is what I do.

iIntegerValue =
CInt(DirectCast(Me.StaffDataGridView.CurrentRow.DataBoundItem,
DataRowView).Row.Item("StaffID"))

In my case I just wanted to store the key into an integer value from the
Staff Table, from
the StaffID column from the record.

But I think your looking for this:
Me.StaffDataGridView.CurrentRow.DataBoundItem,
DataRowView).Row.Item("StaffID")

That is how you get the data from the DataGrid.

But I also want to be able to handle the row at the time it is selected.
Either when selecting the whole row or putting a radio button as a column
and whenever the user presses the button, an event is triggered and I can
load the document from the disk to the textbox.

I don't want to select the row and then press another button to handle the
event. Some type of onSelection event.

Thanks,

Tom
 
M

Miro

Cannot you use the DataGrid onRowEnter event ?

This gets triggered when the Row is selected.
 
T

tshad

Miro said:
Cannot you use the DataGrid onRowEnter event ?

This gets triggered when the Row is selected.

Maybe.

How would I code that?

On a button Click event you have to have:

Public sub something_Click(o as object, e as Eventargs) handles
something_click

And I am not sure if I need to set an event somewhere. I usually set it by
double clicking the button and the event just shows up.

But you can't do that for the DataGrid.

Thanks,

Tom
 
M

Miro

Sure u can... just a double click in a different spot :)

Click once on your data grid
Go to the Properties and you should see a "lightning bolt" icon.

Clicking on that will show you all the events for the Object you are looking at
( in this case the datagrid )

Scroll to one and double click on it.

The only other "Double click" way to access an event is to double click on the Datagrid,
and then in the top there are two drop down boxes. Drop the right one down, and thats
your events for the object thats displayed in the left drop down box.

M.
 
T

tshad

Miro said:
Sure u can... just a double click in a different spot :)

Click once on your data grid
Go to the Properties and you should see a "lightning bolt" icon.
I don't see any lightning bolt icon in Properties.
Clicking on that will show you all the events for the Object you are
looking at
( in this case the datagrid )

Scroll to one and double click on it.

The only other "Double click" way to access an event is to double click on
the Datagrid,
and then in the top there are two drop down boxes. Drop the right one
down, and thats
your events for the object thats displayed in the left drop down box.

This one worked and as you said showed DataGrid in the left hand dropdown,
but in the right hand dropdown all the events there have lightning bolts.
But there is no RowEnter event.

Also, when you double click it you get a "DataGrid1_Navigate" event and if I
select something - I will get that event as well.

Tom
 
M

Miro

The "events" button / "lightning bolt" button does dissapear if you are in the code window.
See if you see the "events" button in the properties box when viewing the "form". Not the
code.

If not...
lets see if vb.net 2005 has changed this quite a bit.

here is the "sub" that gets created for the event.

Private Sub StaffDataGridView_RowEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles StaffDataGridView.RowEnter

End Sub


grab that and throw it into your code, put a msgbox in it, and see if it compiles / executes.
It should hit every time you move a row on the datagrid.

Let me know

M.
 
J

Jack Jackson

The DataGridView control is completely different from the DataGrid,
which is apparently what tshad is using.
 
T

tshad

Jack Jackson said:
The DataGridView control is completely different from the DataGrid,
which is apparently what tshad is using.

You're right.

I didn't event notice that it was the DataGridView.

Tom
 
T

tshad

Miro said:
The "events" button / "lightning bolt" button does dissapear if you are in
the code window.
See if you see the "events" button in the properties box when viewing the
"form". Not the
code.

If not...
lets see if vb.net 2005 has changed this quite a bit.

here is the "sub" that gets created for the event.

Private Sub StaffDataGridView_RowEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
StaffDataGridView.RowEnter

End Sub


grab that and throw it into your code, put a msgbox in it, and see if it
compiles / executes.
It should hit every time you move a row on the datagrid.

Let me know

Nope.

Says that the DataGridViewCellEventArgsdoesn't is not defined.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top