I need a basic grid type control - not a data bound bloated control

J

John Edens

I'm at a loss to figure out how to use VB NET to create a simple, unbound
grid - style Windows control to display data.

I'm not interested in data- binding, sql, or OLEdb.

I just want to define the size of a two dimensional grid, be able to
populate it programmatically, and have it return click events when I click
on a cell.

Is this too much to ask for?
 
M

Mona

Hi John,

The following example populates a datagrid control programmatically. Simple place a DataGrid control on the form and use this code.

Dim Tbl As New DataTable

'This is the column type which will be added to the Datatable

Dim Col1 As New DataColumn

Dim Col2 As New DataColumn

Dim Col3 As New DataColumn

'Declare a procedure to format the datatable

Private Sub FormatGrid()

'Define column 1

'Specify the column data type

Col1.DataType = System.Type.GetType("System.Int32")

'This name is to identify the column

Col1.ColumnName = "No"

'This will enable automatically increment the data

Col1.AutoIncrement = True

'This value will be displayed in the header of the column

Col1.Caption = "No"

'This will make this column read only,Since it is autoincrement

Col1.ReadOnly = True

'Now add this column to the datatable

Tbl.Columns.Add(Col1)

'Repeat the same procedure

'Define Column 2

Col2.DataType = System.Type.GetType("System.String")

Col2.ColumnName = "Name"

Col2.Caption = "Name"

Col2.ReadOnly = False

Tbl.Columns.Add(Col2)

'Define Column 3

Col3.DataType = System.Type.GetType("System.String")

Col3.ColumnName = "Address"

Col3.Caption = "Address"

Col3.ReadOnly = False

Tbl.Columns.Add(Col3)

'BIND THE DATATABLE TO THE DATAGRID

DataGrid1.DataSource = Tbl

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'now call this procedure in the load event of the form

FormatGrid()

'To add rows dynamically in the click event of the button,

'include the code in the click event of the btnAdd

'Create a row type variable

Dim r As DataRow

'Add a new row

r = Tbl.NewRow()

'Specify the col name to add value for the row

r("Name") = "ManojRajan"

r("Address") = "New Delhi"

'Add the row to the tabledata

Tbl.Rows.Add(r)

End Sub

Hope this helps.

Thanks

Mona [GrapeCity]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top