Tag Archives: DataGrid

How to create a Datatable programatically in VB.NET

Dim dt as new DataTable   
        ' Add the columns which you need in your datatable
        dt.Columns.Add("SNo", GetType(Integer))  '1
        dt.Columns.Add("Details", GetType(String))  '2
        dt.Columns.Add("Qty", GetType(String))  '3
        dt.Columns.Add("UnitRate", GetType(Double)) '4
        dt.Columns.Add("ApplyTax", GetType(Boolean))  '5
        dt.Columns.Add("ExTaxTotal", GetType(Double))  '6
        dt.Columns.Add("Tax", GetType(Double))  '7
        dt.Columns.Add("InclTaxTotal", GetType(Double)) '8
       
        ' Create a new row for your table
        Dim drow = dt.NewRow()

        ' Now add the items in your newly created row
        drow.Item(0) = 1
        drow.Item(1) = "My Details go here"
      
        drow.Item(2) = 1
        drow.Item(3) = 234
        drow.Item(4) = 1
        drow.Item(5) = drow.Item(2) * drow.Item(3)   ' You can also calculate the values
        drow.Item(6) = drow.Item(2) * 0.1
        drow.Item(7) = drow.Item(5) * drow.Item(6)


        ' Now add this row to the table
        dt.Rows.Add(drow)

        ' If you want you can display this  data in a datagrid  
        Me.DataGridView1.DataSource = dt

Using Grid.MVC in MVC4 using VB.NET

There are numerous example for C3 but I had to spend quite some time to adapt it to my project using VB.NET

 

Hope it can be of help to you.

I assume that you have

already added the GridMVC package from NUGET.

Here are step by step guide

Controller

No change needed , see a sample below,

Function Index() As ActionResult
Return View(db.Categories.ToList())
End Function

View

Add the following import at the top of your view

@imports  GridMVC.html

 

You would need to reference the following also

 

Add the stylesheets (check the location after you install the GridMVC package)

<link href=”~/Content/Gridmvc.css” rel=”stylesheet” />
    <link href=”~/Content/gridmvc.datepicker.min.css” rel=”stylesheet” />
For your paging support

Install the PagedList package also

Once you have installed this package then add the following reference to your view, this will help you format your paging buttons

properly.

 <link href=”~/Content/PagedList.css” rel=”stylesheet” />

Add the following code for your Grid to appear – In the following examples my Model has three fields – CatId, CatName

and ParentCatId. Make changes to suit your needs.

@Html.Grid(Model).Columns(Function(columns)
                                   
                              columns.Add(Function(o) o.CatID).Titled(“Cat #“).SetWidth(40)
                                 
                              columns.Add(Function(o) o.CatName).Titled(“Category“).SetWidth(100)
                                  
                              columns.Add(Function(o) o.ParentCatID).Titled(“Parent Cat #“).SetWidth(40)
                          
                          End Function
).WithPaging(10).Sortable(True)

 

 

 

How to fill a DataGridView using Entity Model in VB.NET

Here is a quick way of filling up and displaying the records from a table in a DataGridView

This post assumes that you already have created a EntityModel in your project

My Entity Model is called SuperHREntities and I have a table called Employees in it

The trick is to you use the .ToArray() after your table name in the following example to display the records.  If you simply try to use db.Employees then the records will not be displayed.

 

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim db As New SuperHREntities
DataGridView1.DataSource = db.Employees.ToArray()

End Sub

 

 Click on the Image to see it in full screen.

How to fill DataGridViewUsing Entity Model