By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,289 Members | 3,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,289 IT Pros & Developers. It's quick & easy.

Selecting row from DataGrid

P: n/a
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
Oct 25 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.Da taBoundItem, 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.

tshad wrote:
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

Oct 25 '07 #2

P: n/a
Tom,

I think that this sample comes the most in your direction. As written it is
for a DataGridView (a new control in version 2005), however just changing
that to DataGrid will make this workd.

http://www.vb-tips.com/dbpages.aspx?IA=DG0

Cor

Oct 26 '07 #3

P: n/a
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:5B**********************************@microsof t.com...
Tom,

I think that this sample comes the most in your direction. As written it
is for a DataGridView (a new control in version 2005), however just
changing that to DataGrid will make this workd.

http://www.vb-tips.com/dbpages.aspx?IA=DG0
There is a list of samples on this page but nothing on how to respond to a
users selection of a row.

Tom
>
Cor

Oct 26 '07 #4

P: n/a
Sorry

Be aware that this goes automaticly as the users scroll through the grid

http://www.vb-tips.com/CurrencyManager.aspx

Cor
Oct 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.