• 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.

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

1UP

Member
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
  • No one is chatting at the moment.
  • Chat Bot:
    cPwqA is our newest member. Welcome!
  • Chat Bot:
    keonhacaifootball is our newest member. Welcome!
  • Chat Bot:
    QM|T_JinX has joined the room.
  • Chat Bot:
    Keonhacai5vip11 is our newest member. Welcome!
  • Chat Bot:
    smoore99 is our newest member. Welcome!
  • Chat Bot:
    Pagliosa is our newest member. Welcome!
  • Chat Bot:
    QM|T_JinX has joined the room.
  • Chat Bot:
    prototypefox is our newest member. Welcome!
  • Chat Bot:
    maogege is our newest member. Welcome!
  • Chat Bot:
    ShadowPsy974 is our newest member. Welcome!
  • Chat Bot:
    Ghost8099 is our newest member. Welcome!
  • @ Ghost8099:
    Yurrrrrr
  • @ Ghost8099:
    Can we get a new link here brotha
  • Chat Bot:
    Mason Fo has left the room.
  • Chat Bot:
    QM|T_JinX has joined the room.
  • Chat Bot:
    wzxcvcv is our newest member. Welcome!
  • Chat Bot:
    cnood is our newest member. Welcome!
  • Chat Bot:
    zoumar is our newest member. Welcome!
  • Chat Bot:
    bestsmmlike is our newest member. Welcome!
  • Chat Bot:
    josuelton silva is our newest member. Welcome!
  • Chat Bot:
    josuelton silva has posted a new reply in the thread "Console ID #8671".
  • Chat Bot:
    ideasforlifetv is our newest member. Welcome!
  • Chat Bot:
    QM|T_JinX has joined the room.
  • Chat Bot:
    Christo has joined the room.
      Chat Bot: Christo has joined the room.
      Back
      Top