Wednesday, August 11, 2010

How do I access a particular cell in a datagrid in Visual Basic 6?

Suppose I want to access/read/check the data in the first row but second column...How do I do it?ThxHow do I access a particular cell in a datagrid in Visual Basic 6?
This first section explains how to retrieve the value of a cell located in the first row's ';Price'; column.





1) Get a reference to the column of interest. For example, if the column that you are interested in is a field named ';Price'; then, the following line will retrieve a reference:





Dim gCol as MSDataGridLib.Column





Set gCol = DataGrid1.Columns(';Price';)





2) Now lets display the contents of the Price value in the first row.





'The Bookmark property sets or returns the bookmark to the currrent record.


'Because the DataGrid control is always bound to an ADO data source,


'The Bookmark property is often more useful than the Row property.





'IMPORTANT


'Unfortunately, the Answers editor doesn't properly display


'information that is enclosed with two sets of parentheses.


'The Answers editor cuts off the text).


'I had to workaround the problem by using quote marks, instead of the necessary parentheses.





'Please replace the both double quote marks below with an opening and a closing parentheses.


Label1 = gcol.lCellValue';RowBookmark(0)';


______________________________________鈥?br>




There are several ways to access the value of a DataGrid cell in Visual Basic 6. Some are properties of the DataGrid itself. Others are properties of the DataGrid's Column object. You can also get a cell's value from methods of the Column object.





This next line retrieves the value of the cell located in the second column of the current row:





DataGrid1.Columns(1).Value





------





'This next line stores the value entered by the user.





Dim newCellText as String





newCellText = DataGrid.Text





______________________________________鈥?br>




______________________________________鈥?br>







The DataGrid control has a Row and Col run-time property, which set or return the position of the cell in the focus rectangle. The first row and the leftmost column returns zero values. Once you make a given cell the current cell, you can retrieve and modify its content using the DataGrid's Text property.





Each column exposes the Text and Value properties: The former sets or returns the text displayed in the column for the current row, while the latter is the actual value in the column for the current row before it is formatted for display to the user. The Column object also exposes the CellText and CellValue methods, which return the contents of a cell in the column, given its bookmark (see above example).





The DataGrid object exposes two methods that let you access the bookmark of any row in the control. GetBookmark() returns a bookmark of a row relative to the current row: GetBookmark(0) is the same as the Bookmark property, GetBookmark(-1) is the bookmark or the row preceding the current row, GetBookmark(1) is the bookmark of the row following the current row, and so on. The other available method, RowBookmark(), returns the bookmark of any visible row: RowBookmark(0) is the bookmark of the first visible row, and RowBookmark(VisibleRows-1) is the bookmark of the last visible row.





The bookmark of the first row is also returned by the FirstRow property. The LeftCol property holds the index of the first visible column, so you can programatically display the upper left corner of the grid.

No comments:

Post a Comment