Datagrid as a map?

Cajunspirit

Member
Joined
Sep 29, 2008
Messages
19
Programming Experience
Beginner
Hi to all, and thank you for reading.

I have a project due here and the project requires me to use a datagrid to plot cells with respect to a map.

Now we were advised to use a separate form which is transparent with the map on it, which is then transposed over the datagrid, to assess where on the map the points highlight exactly.

Now I am able to get the datagrid and the transparent form, however, I can't seem to get the datagrid to read the points out of the text file and inevitably properly place them within the cells of the datagrid.

The text file must be read into an array.

Is there any kind of guidance I can get for this?
The project is due on the 26th January, 2009.
 
Alright... I am trying to get one line of a text file to be loaded into one row of a datagrid...

Right now the code I have simply inputs the total number of characters in the line of text, as opposed to what was written.

How do I solve this?
 
I'm a little confused as to what you're asking. In your 1st post you say you have 'points' in a text file.

This would suggest to me that you've got a file like:

VB.NET:
50,100
50,50
100,100
150,150
225,200
200,100
5,5

And that you want some formatting done to a DataGridViewCell if it contains that point. In the example below I've taken the sample file listed above and read it into a List(Of Point). In the cell painting event I'm checking if a cell contains one of the points in the list and changing the background color to red if true.

Since you've listed .NET 3.5 I'm assuming you mean DataGridView and not DataGrid.

VB.NET:
Public Class Form1

	Dim locations As New List(Of Point)

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles MyBase.Load

		For i As Integer = 0 To 10
			Me.DataGridView1.Columns.Add(CStr(i), CStr(i))
			Me.DataGridView1.Rows.Add(New DataGridViewRow)
			Me.DataGridView1.Columns(i).Width = 25
			Me.DataGridView1.Rows(i).Height = 25
		Next

		Me.DataGridView1.RowHeadersVisible = False
		Me.DataGridView1.ColumnHeadersVisible = False
		Me.DataGridView1.AllowUserToResizeColumns = False
		Me.DataGridView1.AllowUserToResizeRows = False

		For Each line As String In IO.File.ReadAllLines("C:\Temp\locations.txt")
			Dim pt As New Point
			pt.X = line.Split(",")(0)
			pt.Y = line.Split(",")(1)
			locations.Add(pt)
		Next

	End Sub

	Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
	Handles DataGridView1.CellPainting

		For Each pt As Point In locations
			If e.CellBounds.Contains(pt) Then
				e.CellStyle.BackColor = Color.Red
			End If
		Next

	End Sub

End Class

In your second post you're talking about loading a line of text to a DataGridViewRow which seems like a completely different concept.
 
All right... well what is required of the project is for the program to take 3 values from the user.

Type of crime, Location (two integers) and Time of sighting.

I am able to do this successfully, now all I need to do is to let it be read into an array and sort by the time of the incident.

Then from the array, load the occurrences into the Datagridview.

Thank you so much for the code you posted, really helped open up my eyes.
 
I can help you get started parsing your file into a list and sorting it. Since I don't know what your file looks like I made this one up.

VB.NET:
Hit and Run;50,50;1/21/2009 3:05:52 PM
Speeding;100,100;1/3/2009 8:52:25 AM
Armed Robbery;10,200;1/22/2009 4:12:34 PM

Here's a sample class to organize the data into logical units. Notice the implentation of IComparable and the CompareTo method. You can read up on creating a custom CompareTo method but this one will suffice for this example.

VB.NET:
Public Class CrimeInfo
	Implements IComparable

	Private _crimeType As String
	Public Property CrimeType() As String
		Get
			Return _crimeType
		End Get
		Set(ByVal value As String)
			_crimeType = value
		End Set
	End Property

	Private _location As Point
	Public Property Location() As Point
		Get
			Return _location
		End Get
		Set(ByVal value As Point)
			_location = value
		End Set
	End Property

	Private _sightingTime As DateTime
	Public Property SightingTime() As DateTime
		Get
			Return _sightingTime
		End Get
		Set(ByVal value As DateTime)
			_sightingTime = value
		End Set
	End Property

	Public Function CompareTo(ByVal obj As Object) As Integer _
	 Implements System.IComparable.CompareTo

		If TypeOf obj Is CrimeInfo Then
			Dim temp As CrimeInfo = CType(obj, CrimeInfo)
			Return SightingTime.CompareTo(temp.SightingTime)
		Else
			'TODO: Error handling
		End If

	End Function
End Class

Rather than an array this example uses a List(Of CrimeInfo). For each line in the file create an instance of the CrimeInfo class and assign the field information. Since Location is of type Point and SightingTime is of type DateTime you need to convert them from String first. Finally add it to the List(Of CrimeInfo).

Once you've gone through the file you can call Sort to take advantage of the CompareTo function you set up in your class.

VB.NET:
Public Class Form1

	Dim crimeList As New List(Of CrimeInfo)

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles MyBase.Load

		Dim pc As New System.Drawing.PointConverter()

		For Each line As String In IO.File.ReadAllLines("C:\Temp\crimeexample.txt")

			Dim fields() As String = line.Split(";")

			Dim crime As New CrimeInfo
			crime.CrimeType = fields(0)
			crime.Location = CType(pc.ConvertFromString(fields(1)), Point)
			crime.SightingTime = CType(fields(2), DateTime)
			crimeList.Add(crime)

		Next

		crimeList.Sort()

	End Sub

End Class
 
Wow, thanks... I am really catching on to your code here.

However, I am required to use an array...

I had the class implemented already from research, I modified it to better suite what you wrote, I can not begin to thoroughly thank you enough for the time you are giving.

I am running my own modification of your codes and mines...
Not getting anything displayed on the Datagridview, no source.

Form load
VB.NET:
Private Sub DataGrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim pc As New System.Drawing.PointConverter()

        For Each line As String In IO.File.ReadAllLines("C:\Crime.txt")

            Dim fields() As String = line.Split(";")

            ' Dim Crimes As New ArrayList(CrimeClass)
            Dim Crime As New CrimeClass
            crime.Crime = fields(0)
            crime.Location = CType(pc.ConvertFromString(fields(1)), Point)
            crime.Time = CType(fields(2), DateTime)
            'Crimes.Add(crime)

        Next

        ' Crimes.Sort(Crime.time)


    End Sub

Class
VB.NET:
Public Class CrimeClass
    Private C_CrimeType As String
    Private Location_ As Point
    Private T_Time As Date

    Public Property Crime() As String
        Get
            Return C_CrimeType
        End Get
        Set(ByVal Value As String)
            C_CrimeType = Value
        End Set
    End Property

    Public Property Location() As Point
        Get
            Return Location_
        End Get
        Set(ByVal Value As Point)
            Location_ = Value
        End Set
    End Property

    Public Property Time() As DateTime
        Get
            Return T_Time
        End Get
        Set(ByVal Value As DateTime)
            T_Time = Value
        End Set
    End Property

End Class

Save to text file
VB.NET:
  Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("c:\Crime.txt", True)
        file.WriteLine(cbxSight.Text & ";" & txtLX.Text & "," & txtLY.Text & ";" & Date.Now)
        file.Close()
    End Sub

Yeah... so essentially I need to use an array, and input the values from the text file through it, unto the datagrid, each field with it's own column
 
I'm not sure why you're required to have an array from the code and description of what you're providing. I'll take your word for it and suggest you use the List(Of CrimeInfo) and call the ToArray member function when you need to pass the array.

I'm able to display the data just fine using:

VB.NET:
Me.DataGridView1.DataSource = crimeList

It also worked setting the DataSource to an ArrayList and Array.
 
Thank you!
You are the best!

Incidentally my name is Mathieu. I am one to believe Matthews are nice people.
Many thanks once again!
 
Not a problem. You should change 1 line of the code I gave you to remove an implicit string to char conversion.

VB.NET:
 Dim fields() As String = line.Split(";")

should be:

VB.NET:
 Dim fields() As String = line.Split(";"c)
 
Back
Top