Braden Putt
Braden Putt

Reputation: 61

DataGridView with columns as strings AND columns as integers

I have a datagridview with 5 columns (Venue, Location, Size, Capacity, Hire Cost). I get the data from a text file. It all works fine except what i would like is for the user to be able to sort the capacity and hire cost by clicking on the column headers. The problem though is that because of the way it is coded all of the columns are strings. What i need is to have the first 3 columns as strings and the last two as integers, but i'm not sure how i should be modifying my code to achieve this.

I currently have the following:

Dim row As String() = New String() {vi.venue_name, vi.venue_location, vi.venue_size, String.Format("{0:n0}", vi.venue_capacity), "$" & String.Format("{0:n0}", vi.venue_hirecost)}
DataGridView1.Rows.Add(row)

How do i modify this so that only the first three columns are strings and vi.venue_capacity and vi.venue_hirecost columns are integers?

EDIT: I have sorted this. Was a very easy solution. I simply changed the line:
Dim row As String() = New String() {vi.venue_name, vi.venue_location, vi.venue_size, String.Format("{0:n0}", vi.venue_capacity), "$" & String.Format("{0:n0}", vi.venue_hirecost)}

to:

 DataGridView1.Rows.Add(vi.venue_name, vi.venue_location, vi.venue_size, vi.venue_capacity, vi.venue_hirecost)`

It now works perfectly! :-)

Upvotes: 0

Views: 869

Answers (2)

Braden Putt
Braden Putt

Reputation: 61

I have sorted this. Was a very easy solution. I simply changed the line:

Dim row As String() = New String() {
    vi.venue_name, 
    vi.venue_location, 
    vi.venue_size, 
    String.Format("{0:n0}", 
    vi.venue_capacity), 
    "$" & String.Format("{0:n0}", 
    vi.venue_hirecost)
}

to:

DataGridView1.Rows.Add(
    vi.venue_name, 
    vi.venue_location, 
    vi.venue_size, 
    vi.venue_capacity, 
    vi.venue_hirecost
)

It now works perfectly! :-)

Upvotes: 1

Sreenath Ganga
Sreenath Ganga

Reputation: 736

Better option will be using a datatable to do the same..Please read the comments in code to understand what is done. Then bind the datatable to Datagridview..

''' <summary>
    ''' Sub for 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub GetData()

        ''Create a Dummy Datatable

        Dim tempCompoTable As DataTable = New DataTable()

        ''Add Columns
        tempCompoTable.Columns.Add(New DataColumn("Venue", Type.GetType("System.String")))
        tempCompoTable.Columns.Add(New DataColumn("Location", Type.GetType("System.String")))
        tempCompoTable.Columns.Add(New DataColumn("Size", Type.GetType("System.String")))
        tempCompoTable.Columns.Add(New DataColumn("Capacity", Type.GetType("System.Double")))
        tempCompoTable.Columns.Add(New DataColumn("Hire", Type.GetType("System.Double")))
        tempCompoTable.Columns.Add(New DataColumn("Cost", Type.GetType("System.Double")))


        Dim lines() As String = IO.File.ReadAllLines("venues.txt")
        For Each line In lines
            If Not line.Trim.Equals("") Then
                Dim strArr() = line.Split(":")

                ''Createnewrow
                Dim drworg As DataRowView = tempCompoTable.DefaultView.AddNew()
                drworg("Venue") = strArr(0)
                drworg("Location") = strArr(1)
                drworg("Size") = strArr(2)
                drworg("Capacity") = Val(Trim(strArr(3) & ""))
                drworg("Hire") = Val(Trim(strArr(4) & ""))
                drworg.EndEdit()

                ''if you want
                'Dim vi As New VenueInfo()
                'vi.venue_name = strArr(0)
                'vi.venue_location = strArr(1)
                'vi.venue_size = strArr(2)
                'vi.venue_capacity = strArr(3)
                'vi.venue_hirecost = strArr(4)


            End If
        Next

        grd.AllowUserToAddRows = False
        grd.DataSource = tempCompoTable

    End Sub

If you want instead of directly binding from array you can bind to value_info and then loopthrough it to add rows to datatable

Upvotes: 1

Related Questions