Post: [ASP.NET /VB.NET] Populate a Datagrid view without a database
07-22-2014, 05:32 PM #1
Default Avatar
Oneup
Guest
(adsbygoogle = window.adsbygoogle || []).push({}); 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

    
<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
    

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:

You must login or register to view this content.

The following user thanked Oneup for this useful post:

theDaftDev
08-01-2014, 06:05 PM #2
Pichu
RIP PICHU.
Originally posted by 1UP View Post
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

    
<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
    

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:

You must login or register to view this content.


Best explanations are when you comment your source and explain as you go.

example:

//Creates new DataTable
//Gets users Operating System
//Gets users Total Memory
//Gets Users Operating System

The JIT compiler will ignore and remove of all of the commenting when the program is compiled. Added commentation only means the code is much more readable (Given you are precise with what you are commenting on).

It also helps with finding information or parts of code you need to search for quickly.

Ctrl+F finding thing becomes even more helpful when you have commentation that contains a keyword whereas your method may not.

Other than that, I don't see a whole lot wrong here although I'd have gone about this a tad bit differently; though what it is for a simple concept is good.
08-01-2014, 07:46 PM #3
Default Avatar
Oneup
Guest
Originally posted by Pichu View Post
Best explanations are when you comment your source and explain as you go.

example:

//Creates new DataTable
//Gets users Operating System
//Gets users Total Memory
//Gets Users Operating System

The JIT compiler will ignore and remove of all of the commenting when the program is compiled. Added commentation only means the code is much more readable (Given you are precise with what you are commenting on).

It also helps with finding information or parts of code you need to search for quickly.

Ctrl+F finding thing becomes even more helpful when you have commentation that contains a keyword whereas your method may not.

Other than that, I don't see a whole lot wrong here although I'd have gone about this a tad bit differently; though what it is for a simple concept is good.

Except the code blocks do not parse out code and color it so doing comments helps no one in the case since they are more then likely not going to read them. I know what comments are and why I don't post them in a thread when I am explaining things.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo