 |
| VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 7th, 2004, 11:49 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Accessing the data within a datagrid
I would like to access the data within a datagrid eg
if datagrid1.column2.value = false then
end if
What is the syntax for this?
Thanks in Advance
Louisa
|
|

January 7th, 2004, 12:00 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
What row of data would you like to access?
What are you trying to achieve with this?
The syntax will be very different depending on what you are attempting to do.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

January 7th, 2004, 12:27 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What I am trying to do is change the colour of the rows depending upon the values in certain fields. For example if it is a week before a date in the date field I would like the row to go red to warn the user that the date is approaching.
|
|

January 7th, 2004, 12:48 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This may not be exactly what you need, though may help. It is for a flexiGrid and is VB6
Public Sub fnColourOngoingIssues()
Dim iLoop As Single
Dim dtPlaceHolder As Double
Dim iCol As Single
Dim iFlag As Single
On Error GoTo fnColourOngoingIssuesErr
iCol = 4
iFlag = 7
With vsOngoingIssues
For iLoop = 1 To .Rows - 1
If IsDate(.TextMatrix(iLoop, iCol)) Then
dtPlaceHolder = DateDiff("d", Date, Format(.TextMatrix(iLoop, iCol), "dd/mm/yy"))
Select Case dtPlaceHolder
Case Is > iFlag: .Cell(flexcpForeColor, iLoop, iCol) = vbBlack 'In the future
Case 0 To 7: .Cell(flexcpForeColor, iLoop, iCol) = vbBlue 'Needs to be dealt with
Case Is = 0: .Cell(flexcpForeColor, iLoop, iCol) = vbBlack 'Today/Overdue
Case Is < 0: .Cell(flexcpForeColor, iLoop, iCol) = vbRed
Case Else 'Should never reach here
End Select
End If
Next
End With
Exit Sub
fnColourOngoingIssuesErr:
fnDisplayVBError
End Sub
Ian
|
|

January 7th, 2004, 01:06 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Try this out:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
Dim objItem As DataGridItem = e.Item
'Do NOT deal with header, footer
If Not(objItem.ItemType = ListItemType.Header And objItem.ItemType = ListItemType.Footer) Then
If CType(objItem.Cells(0).Text, Date) > DateTime.Now.AddDays(-7) Then
objItem.BackColor = System.Drawing.ColorTranslator.FromHtml("FF0000")
End If
End If
End Sub
You'll need to set the proper cell index to match the cell with your date in it.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

January 7th, 2004, 01:07 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Note: The example I provided is for a datagrid in ASP.net, you might need to change it to work properly for a windows form datagrid.
|
|

January 8th, 2004, 05:32 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
A datagrid in VB.Net does not have a cell property for me to be able to index, what property what can I use instead?
Thanks again
Louisa
|
|

January 8th, 2004, 12:44 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
datagrid1.item(rowindex, columnindex)
For example:
datagrid1.Item(0,0)
would return the value of the first cell of the first row. I believe this is what you are looking for.
|
|

January 12th, 2004, 07:32 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Perfect
Thank you very much
Louisa
|
|
 |