Matt Andrzejczuk
Matt Andrzejczuk

Reputation: 2066

How do you create and add data to gridview object? VB.NET

I want to create a grid view object with two columns: EventName and Date.

I'm working in VB and my webapp will read through a txt file to get this data that will be added to the gridview. The data in the text file will look something like this:

76ers vs Timberwolves
2/20/2013

76ers vs Heat
2/23/2013

76ers vs Knicks
2/24/2013

76ers vs Warriors
3/2/2013

So that after reading through the list, my grid view should look something like this:

EVENT NAME                 DATE
76ers vs Timberwolves      2/20/2013
76ers vs Heat              2/23/2013
76ers vs Knicks            2/24/2013
76ers vs Warriors          3/2/2013

This is the code I used for reading the data from the text file to directly render it into a calendar object:

Sub DayRender(ByVal source As Object, ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender


Dim FILENAME As String = Server.MapPath("EventsList3.txt")


Dim objStreamReader As StreamReader
objStreamReader = File.OpenText(FILENAME)


Dim contents(0 To 999) As String
Dim month(0 To 999) As Integer
Dim day(0 To 999) As Integer
Dim year(0 To 999) As Integer

Dim i As Integer = 0

While objStreamReader.EndOfStream = False
    objStreamReader.ReadLine()
    contents(i) = objStreamReader.ReadLine
    month(i) = objStreamReader.ReadLine
    day(i) = objStreamReader.ReadLine
    year(i) = objStreamReader.ReadLine
    i = i + 1
End While


Dim j As Integer = 0

While j < 10
    If e.Day.Date.Day = day(j) And e.Day.Date.Month = month(j) And e.Day.Date.Year = year(j) Then
        e.Cell.Controls.Add(New LiteralControl(ChrW(60) & "br" & ChrW(62) & contents(j)))
    End If
    j = j + 1
End While

objStreamReader.Close()

End Sub

The text file I used for the code above looked like this:

76ers vs Timberwolves
2
20
2013

76ers vs Heat
2
23
2013

76ers vs Knicks
2
24
2013

76ers vs Warriors
3
2
2013

76ers vs Celtics
4
2
2013

76ers vs Heat
3
13
2013

76ers vs Hawks
3
6
2013

76ers vs Magic
3
10
2013

76ers vs Bulls
2
28
2013

76ers vs Raptors
1
18
2013

Now I need to do almost the same thing, except now instead of sending the information to a calendar object, I need the information to be added into a grid view object. Does anyone know a good gridview guide that could give me an extremely easy sample to work off of? My problem is just figuring out using Microsoft's objects since I'm required to use them for my assignment.

I'll be very grateful for a nice sample Gridview, cheers!

Upvotes: 1

Views: 5301

Answers (2)

Andrey Gordeev
Andrey Gordeev

Reputation: 32459

Create a DataTable object, fill it by your data and set the DataTable as DataSource for your GridView. Here is a good example how to do that.

Upvotes: 1

aspiring
aspiring

Reputation: 1647

Since you are asking for GridView tutorial or sample,

It's recommended to fllow a tutorial to get familiar with the object :)

Upvotes: 1

Related Questions