ARTICLE

Hide a DataGrid column using VB.NET

Posted by Praveen Kumar Articles | Visual Basic 2010 November 17, 2004
How do I hide a column in a DataGrid or DataTable?
 
Reader Level:

Q1. How do I hide a column in a DataGrid or DataTable?

Ans.
DataGrid.Columns(index).Visible =
False

See also: Mapping type?

Set DataColumn.MappingType as
MappingType.Hidden

ONE MORE VERSION: GRID ONLY:

The grid part of a DataGrid is represented by a table. The TableStyles property provides to this table. TableStyles property returns an object of type DataGridTableStyle, which provides access to all columns in the form of DataGridColumnStyles collection. The GridColumnStyles property of DataGridTableStyle returns it, from where you can get one DataGridColumnStyle. The DataGridColumnStyle represents the style of a column and you need to set Width property of DataGridColumnStyle to 0.

Something like this:

For Each dgt As DataGridTableStyle In
myDataGrid.TableStyles
Console.WriteLine(dgt.MappingName)
For Each dgc As DataGridColumnStyle In
dgt.GridColumnStyles
dgc.Width = 0
Next
dgc
Next dgt

Q2. How do I make DataSet processing faster?
Q3. How do I save only affected rows of a DataSet?

Ans. SELECT * is the worst thing you can do if you want only few columns and your table has many of them. If you want to apply filter later, you can use the Filter property and set a criteria.

The GetChanges method of DataSet returns the changes made to a DataSet since you last saved. This method retruns data in another DataSet. When you call Fill method of DataAdapter, use the DataSet returned by the GetChanges method.

Here is the code:

' Check for changes with the HasChanges method first.
If (Not myDataSet.HasChanges(DataRowState.Modified))
Then
Return
End
If
' Create temporary DataSet variable.
Dim xDataSet As
DataSet
' GetChanges for modified rows only.
xDataSet = myDataSet.GetChanges(DataRowState.Modified)
' Check the DataSet for errors.
If xDataSet.HasErrors
Then
' Insert code to resolve errors.
End
If
' After fixing errors, update the DBMS with the DataAdapter
' used to create the DataSet.
myOleDbDataAdapter.Update(xDataSet)


Q3. How do I Get Rid of + Sign in DataGrid?

When I fill my DataSet and display it, all I see is a '+' sign to the left of the grid. When I click on the plus sign I then see my table name. Clicking on the table name finally displays my data. I have played around with all the properties that I thought might be related to this, but with no luck. I want my users to see the data right-away without having to do these extra clicks.

Ans. Try filling data from a DataGrid instead of a DataSet. Means set DataGrid's DataSource property as a DataTable, which you can get from DataSet.Tables property. A DataSet is a collection of DataTables and DataGrid assumes that DataGrid may have more than one tables.

I think this should work:

Private
dataGrid1.DataSource = ds.Tables("TableName")

Q4. How do I get a database table's list programmatically?

Ans.
Use GetOleDbSchemaTable method of OleDbConnection for OLE DB data sources and Use table name "sysobjects" for SQL Server.

Q5. Is there a way to get the names of all the columns of a table access (or SQL) without using a dataset?

Ans.

Private sc As StringCollection = New
StringCollection()
Private r As IDataReader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)

Private
drc As DataRowCollection = r.GetSchemaTable().Rows
For Each row As DataRow In
drc
sc.Add(row("ColumnName").ToString())
Next row

share this article :
post comment
 

There is no 'visible' attribute on a data column.

Posted by Paul Abrams Aug 09, 2010

i have accessed records of database in datagrid now i want thatone of the database column which is quantity,i want to divide quantity by 50 and then display result in the new colum which i have also make at that time name final .now how i will display data in that temporary column of the datagrid by dividing the database column record of the datagrid? how?

Posted by imran irshad May 15, 2009

i want that the database which i have accessed through datagrid and display it then i also make a column temporary at that time which is also displaying .now i want that the database column quantity which is previous then today i want to divide that column by 50 and the data become display in the today column.what is the code? thanks

Posted by imran irshad May 15, 2009
Nevron Diagram
Become a Sponsor
MOST LIKED ARTICLE
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor