Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hiding Columns in a Databound Datagrid

Status
Not open for further replies.

tpbjr

MIS
Oct 8, 2004
120
US
Hi,
Does anyone know of an easy way to hide columns in a databound datagrid (Windows form). I programmatically have created the dataadapter, connection, and dataset. How can I programmatically hide the first two columns.
In addition, if the columns are hidden, will I still be able to populate the second one with data while it is hidden.


Thank you for all your help

Tom
 
I am using VB.NET 2003

My code that works with the one of the datagrids looks like this.

Private Sub CreateDataGrid()
Dim strSQL As String
strSQL = "SELECT ID, LoginID, Diagnosis FROM tblDiagnosis "
strSQL = strSQL & "WHERE LoginID = " & gUserID_Number
m_DataAdapter = New OleDb.OleDbDataAdapter(strSQL, gConn)

'Fill the Dataset
m_DataAdapter.Fill(m_DataSet)

'Bind the Datagrid to the DataSet's first DataTable
dgDiagnosis.DataSource = m_DataSet.Tables(0)

End Sub

Thank you for all your help

Tom
 
I have found that the best way to handle formatting like this in a DataGrid is to set the AutoGenerateColumns Property to False and use the Property Builder to specify the columns and their attributes... this also makes setting the column properties at runtime possible. At least that is the only way I have gotten it to work well. ;o)

Senior Software Developer
 
I do not see a AutoGenerateColumn property available.

Man, I thought VB would be getting easier but this .NET is a step backwards.

Thank you for all your help

Tom
 
From the Form Designer, click on the datagrid. Then in the properties window, you can either alpha-sort for it or scroll to the Behavior section.

The Property Builder is a link in the bottom of the Property Window.

... and I would agree to some extent... but overall ASP.NET is WAY easier than that old ASP Script Stuff. LOL!


Senior Software Developer
 
In VS2005 it is much more straight forward where you can access the gridview's columns and set the Visible property. In VS2003 you have to use table styles. Here is some sample code I use, you can modify it to meet your needs:
Code:
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Vehicles"
dgVehicles.TableStyles.Add(tableStyle)
dgVehicles.TableStyles("Vehicles").GridColumnStyles("VehicleID").Width = 0 'this makes the column non-visble 'but still accessable from code
Jim
 
I really appreciate all the support. However, I cannot get this to work. I am so disappointed in .NET. I had no problems programming in VB6. Even the help file information does not make sense to me. Microsoft must have gotten antirely different crew to write the help file. It is not intuitive at all. I keep getting different errors. I try to use different names in place of your Vehicle example but have no luck. Please help me.

Private Sub CreateDataGrid()
Dim strSQL As String
strSQL = "SELECT ID, LoginID, Diagnosis FROM tblDiagnosis "
strSQL = strSQL & "WHERE LoginID = " & gUserID_Number
m_DataAdapter = New OleDb.OleDbDataAdapter(strSQL, gConn)

'Fill the Dataset
m_DataAdapter.Fill(m_DataSet)

Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "m_DataSet"
dgDiagnosis.TableStyles.Add(tableStyle)
dgDiagnosis.TableStyles("m_DataSet").GridColumnStyles("ID").Width = 0 'this makes the column non-visble 'but still accessable from code

'Bind the Datagrid to the DataSet's first DataTable
dgDiagnosis.DataSource = m_DataSet.Tables(0)

End Sub

Thank you for all your help

Tom
 
Hi again,
I have tried this bunch of code and it does not error out. However, it has no effect. My first two columns are not hidden and the LoginID field name still displays in the grid column header. Can anyone recommend a good book that will help an idiot like me understand .NET Windows Form programming. In addition, a book for getting started on ASP .NET programming (Intranet and Internet stuff).

Private Sub CreateDataGrid()
Dim strSQL As String
strSQL = "SELECT ID, LoginID, Diagnosis FROM tblDiagnosis "
strSQL = strSQL & "WHERE LoginID = " & gUserID_Number
m_DataAdapter = New OleDb.OleDbDataAdapter(strSQL, gConn)

'Fill the Dataset
m_DataAdapter.Fill(m_DataSet)

Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Diagnosis"

Dim ID As New DataGridTextBoxColumn
ID.MappingName = "ID"
ID.HeaderText = "Auto ID"
ID.Width = 0
tableStyle.GridColumnStyles.Add(ID)

Dim LoginID As New DataGridTextBoxColumn
LoginID.MappingName = "LoginID"
LoginID.HeaderText = "Login ID"
LoginID.Width = 0
tableStyle.GridColumnStyles.Add(LoginID)

Dim Diagnosis As New DataGridTextBoxColumn
Diagnosis.MappingName = "Diagnosis"
Diagnosis.HeaderText = "Diagnosis"
Diagnosis.Width = 150
tableStyle.GridColumnStyles.Add(Diagnosis)

dgDiagnosis.TableStyles.Add(tableStyle)

'Bind the Datagrid to the DataSet's first DataTable
dgDiagnosis.DataSource = m_DataSet.Tables(0)
End Sub

Thank you for all your help

Tom
 
Man, I thought VB would be getting easier but this .NET is a step backwards.

i think the biggest problem is VB.NET is getting to be a real language while VB6 never went over the bounds of scripting technologies. That seems to be the hurdle many that only restricted themselves to scripting languages are having. Going OO that is and having some power to deal with.

VB.NET made VB a language you can actually use and come close to using in a manner that provides easy maintainability and stability. :)

it's just a learning curve. There's no comparison to VB6 and VB.NET. That's why you may be having some troubles. Try to forget VB6 and you'll do better

____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top