Datagridview

The DataGridView control

I found many programers interested about using the Data Grid View, so I made this small tutorial about it.
I'm going to show how to use the Data Grid View independently of a data base. I'll show the best methods that I found while reading and learning about the datagridview control. And I'll also wait for your comments or questions.
In this part I'll explain how to use it with the datagridview and put data on it with the datagridview.datasource using an array and a class.
Understanding the DGV Data Source :
First let's return to a DGV when it's related to a data base or how to relate it.
Simply we use the following code : datagridview.datasource = BindingSource
So the datagridview get the appearence (Columns name) and the data (rows) from the binding source and put it in the datagridview.datasource
But the datagridview don't accept only one type of datasources but many and this will help us to customize it.
Now open a new project and create a new form and place a new datagridview.
Now make a new button and double click to put code on it.
Let's think about what kind of data the datagridview can accept. String (it's impossible) and also integer. But if you try to put a string or integer on the datagridview datasource you won't get an error but simply nothing (and for me that's worth, because when you don't get error you don't know where the problem is!!)
So why not array, arrays are like a table and the datagridview is a table
So put the following code on the button Click action

Dim arr() As String = _
{"a", "aa", "aaa"}
DataGridView1.DataSource = arr

And then run the application, when you'll click the datagridview will display a column "Length" and will give each string length. That's stupid 4 you but the datagridview need more presicion to understand your code.

So let's try more code :

First add the following class

Public Class DGV
Private _row As String
Public Sub New(ByVal row As String)
_row = row
End Sub

Public Property Column() As String
Get
Return _row
End Get
Set(ByVal value As String)
_row = value
End Set
End Property
End Class

and then put the following code on the button click action

Dim arr() As DGV = { _
New DGV("Row 1"), _
New DGV("Row 2"), _
New DGV("Row 3")}
DataGridView1.DataSource = arr

The following code will made a new column in the datagridview named column (the same name as the property name)
You can make rows as much as you want just in the DGV array add a new "New DGV("Whatever you want")"
Now we can make a column and put data on it. So let's advance more.
First you won't use this class as it but you have to customize it for your need.

Public Class DGV
Private _name As String
Private _ID As String

Public Sub New(ByVal id As String, ByVal name As String)
_ID = id
_name = name
End Sub

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public Property ID() As String
Get
Return _ID
End Get
Set(ByVal value As String)
_ID = value
End Set
End Property
End Class

And then. First let's tune the columns numbers. See the public property. We have in this class 2 public property. This means that we'll get 2 columns. If you want more columns then add a new public property. The public property name is important because (as the previous example) it will be the column name.
Now, if you are going to add a column then add a new public property and choose a new for it, for example "comment" then add a new private string (or integer as you like) To be organised name the string _comment, so then it'll be much colmuns you won't get lost.
Now let's move on to the public property.
Add a Get block and put on it Return _comment
Add a Set block (Byval value as String) and put _comment = value
Finally in the new sub put _comment = comment
And then we made a new column.
NB : Very important, the column order depend on the public property order ! We made name then Id then we'll get a datagridview with "name"id" as the order of the class.
Now let's move to the button that we'll use the DGV class to put the data.

Dim arr() As DGV = { _
New DGV("Row 1", "more"), _
New DGV("Row 2", "more"), _
New DGV("Row 3", "more")}
DataGridView1.DataSource = arr

Now let's analyze the following code
New DGV (property1,property2),_
New DGV (property1,property2),_
.....
Very important!! if you have 2 column as in this sample then you musty enter 2 property, if you have 3 column then you must enter 3 property.
If you forget one this will become an error.

Don't forget to comment or suggest if I missed somethings

0 comments:

Post a Comment