How DataGridView in VB .Net Can Be Use for Representating Data when We Don't Want to Use Any DataSource?
I tried searching in google but didn't find any solution then at the end I tried some of the properties that can be use for inserting Data into DataGridView Rows and Columns.
When you will add DataGridView Control into your VB Form, you have to add columns. Now Adding Columns you have many options in the Column Field Type - like whether you want to have it in TextField, Checkbox, Dropdown box, Image, or button. Now depends on your requirement add those fields for each column.
After doing all these things - only thing remaining is how can we insert data or string into the each Row/Column. Lets discuss each property one by one with syntax of using those property in VB .net
First of all you have to find out how many Rows you want to add in that DataGridView, It can be done at the start of the Data filling process or it can be done run time.
This line of code will help you to add Number of Rows in the DataGridView in one shot.
[MyDataGridView is a name of my DataGridView Control]
MyDataGridView.Rows.Add (TotalRows)
If you want to add Rows during the Run Time, you can use below line of code.
MyDataGridView.Rows.Insert (rowIndex, Numbers)
There are few more functions that can be use for inserting Row at run time.
***[Remember one thing, DataGridView Row always starts with ZERO index ]
I prefer to add all Rows/Create Rows at once at the start of the process for representing Data. so once we added require number of Rows into the DataGridView, next task is to insert Text values into the Rows of each Column.
MyDataGridView.Item(ColumnNumber, RowNumber).Value = "YOUR TEXT"
Using the above line of code you can keep on inserting values in each Rows/Columns. These are the basic functions and properties that we use. Now if someone added Button as a Row Type instead of TextField Typeso in this case he/she must be expecting some event to be performed when he/she will click on that button present in the DataGridView.
To Add such a Event Handler.. You need to Event Handler called "CellContentClick". To add such a Event Handler, just open the Form where DataGridView Control is added and double Click on it.
Using e.ColumnIndex and e.RowIndex you can perform the operations and validations.
If you have selected Field type of CheckBox then instead of assigning "String" to the Value property you have to assign Boolean values (True/False) to Check and Uncheck the CheckBox on Checkbox Click under the "CellContentClick" event handler.
I hope this will help you a lot using DataGridView in your VB Project.
Published by Paresh
I love to do Painting, Sketching, Photography and many more things. I started writing on things which interests me. I am not an expert in writing but I am trying to be a good writer :) Most of the time yo... View profile
- The Best Tutorial Sites for Visual Basic.NET
- Columns and Rows !!
- How to Use Win32API in Visual Basic
- How to Read/Write/Delete Microsoft Windows Registry Keys/Values Using VB .Net?
- Introduction to Visual Basic
- Visual Basic: Create a Text Spammer
|
|
- Use of DataGridView Control in VB .net for Representing Data when we are not using any DB
1 Comments
Post a CommentThanks. very useful tip