Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
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
 
Old January 7th, 2004, 11:49 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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


 
Old January 7th, 2004, 12:00 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old January 7th, 2004, 12:27 PM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old January 7th, 2004, 12:48 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old January 7th, 2004, 01:06 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old January 7th, 2004, 01:07 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old January 8th, 2004, 05:32 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old January 8th, 2004, 12:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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.
 
Old January 12th, 2004, 07:32 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perfect

Thank you very much

Louisa






Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing a Cell in a Webform Datagrid hexOffender VB.NET 2002/2003 Basics 1 December 28th, 2006 01:14 AM
Accessing data From a node austinf XML 1 June 7th, 2006 02:16 AM
Accessing data on the clipboard. voneimin Javascript How-To 7 March 5th, 2004 11:30 PM
accessing data from Excel chikodi Servlets 1 July 31st, 2003 12:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.