[ASP.NET /VB.NET] Populate a Datagrid view without a database

  • Hello Guest! Welcome to ConsoleCrunch, to help support our site check out the premium upgrades HERE! to get exclusive access to our hidden content.
  • Unable to load portions of the website...
    If you use an ad blocker addon, you should disable it because it interferes with several elements of the site and blocks more than just adverts.
  • Read Rules Before Posting Post Virus Scans with every program.

1UP

Member
Oct 9, 2014
24
10
63
I wanted to list out some data for a page that wasn't being pulled from a database but I didn't want to write out the table and all that. So I dropped a datagridview on to the webpage and wrote some code in the code behind.

ASP.NET Side of things

Code:
  <asp:GridView ID="grd_Specs" CssClass="footable" runat="server" AutoGenerateColumns="False">
  <Columns>
   
  <asp:BoundField DataField="item1" HeaderText="Pc Specs"  />
  <asp:BoundField DataField="itemstat" HeaderText="" />

  </Columns>
  </asp:GridView>

VB.Net Code
Code:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  PopulatePCGrid()
   
  End Sub

  Dim DT As New DataTable
  Dim DR As DataRow

  Private Sub PopulatePCGrid()
  Dim DS As New DataSet

  DT = New DataTable("items")
  DT.Columns.Add("item1")
  DT.Columns.Add("itemstat")
  DR = DT.NewRow

  AddRowToGrid("Operating System", My.Computer.Info.OSFullName.ToString)
  DR = DT.NewRow
  AddRowToGrid("Total Memory", ((My.Computer.Info.TotalPhysicalMemory / 1024) / 1024).ToString("N0") & " MB")

  grd_Specs.DataSource = DT

  grd_Specs.DataBind()
  End Sub

  Private Sub AddRowToGrid(ByVal description As String, ByVal data As Object)
  With DR
  .Item("item1") = description
  .Item("itemstat") = data
  End With
  DT.Rows.Add(DR)
  End Sub

I will explain this as best as I can.
We create two variables, 1 for the Data Table and 1 for the Data Row.
When we get into PopulatePCGrid we create a new data set and set up the data table to match the gridview layout. If these fields do not match you will get an error on page load. So make sure the fields you want exist in both the gridview and in the code behind.

I wrote a sub proceedure to add rows to the dataTable. This isn't required but it's best to break out code that you will be using over and over again and make them into functions /subs.

So we call the proceedure and pass in description and data. Data is an object simply because it could be anything. I didn't want to limit it to a string or integer because that wouldn't be true (and in fact on the actual page I wrote this for it wasn't true).

We then add that row to the data table and go back to PopulatePCGrid to finish the rest of the code. We create a new Datarow and then continue to add more information to the Data table. After we finish that we bind the datagridview and we end up with something like this:

 
General chit-chat
Help Users
  • @ QM|T:
    nice
  • @ lurch6661:
    all this and more if the price is right
  • @ lurch6661:
    lol
  • @ QM|T:
    how mutch is it
  • @ QM|T:
    that old bo 1 and 2 will be 80 for those old games haha
  • @ lurch6661:
    lol
  • @ QM|T:
    so what you up too
  • @ lurch6661:
    music smoking
  • @ QM|T:
    lol
  • @ lurch6661:
    you ready for the new heist on gta
  • @ lurch6661:
    only here in texas
  • @ lurch6661:
    lol
  • @ QM|T:
    yea are you hahaha
  • Chat Bot:
    thedemonspawn56 has left the room.
  • Chat Bot:
    KzVTz is our newest member. Welcome!
  • @ KzVTz:
    Thanks my brother
  • Chat Bot:
    Jimmy123098 is our newest member. Welcome!
  • Chat Bot:
    Christo has joined the room.
  • Chat Bot:
    joseillo is our newest member. Welcome!
  • Chat Bot:
    IXxKakarotxXI is our newest member. Welcome!
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    VitalMix is our newest member. Welcome!
  • @ Fretyeco192321:
    Went to that store for a XBOX 360 was 1 Hour late so I did not get the XBOX 360 -_-
  • @ Fretyeco192321:
    oh well my offline XBOX 360 emulator will do for now Family Guy Back To The MultiVerse & NBA Street HomeCourt here I come
      @ Fretyeco192321: oh well my offline XBOX 360 emulator will do for now Family Guy Back To The MultiVerse & NBA...