Hide Columns in Datagrid

  • Thread starter Thread starter J
  • Start date Start date
J

J

Ok, they have changed a lot of stuff in VB.net. How in the world do you hide a column on the Datagrid?
 
I have been trying that but am having issues: here is the code I have been
trying:

I have been experimenting with a bunch of different methods but I cannot get
the setbinding to work.

Any ideas?

Private Sub Query_Grid()



Dim strSQL As String

strSQL = "SELECT tblLogged.log_ID, format(tblLogged.time_start,'HH:MM') AS
Start, format(tblLogged.time_end,'HH:MM') AS [End], tblLogged.act_cat AS
Category, tblLogged.act_desc AS Description, tblLogged.user_name,
tblLogged.act_date"

strSQL = strSQL & " FROM(tblLogged)"

strSQL = strSQL & " WHERE(((tblLogged.user_name) = '" & g_User & "') And
((tblLogged.act_date) = #" & String.Format("{0:MM\/dd\/yyyy}",
Me.dtDate_Val.Value) & "#))"

strSQL = strSQL & " ORDER BY tblLogged.time_start, tblLogged.time_end;"

Me.DataGrid1.DataSource = Nothing

Dim AppPath As String = g_ODBC

Dim cn As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source
= " + AppPath)

Dim cmd As New OleDbCommand(strSQL, cn)

Dim da As New OleDbDataAdapter(cmd)

Dim ds As New DataSet

' Dim customersTable As DataTable = New DataTable("Customers")

'' Insert code to set source to populate DataSet.

'' Set DataSource and DataMember with SetDataBinding method.

'Dim member As String

'' The name of a DataTable is Customers.

'member = "Customers"

'DataGrid1.SetDataBinding(myDataSet, member)



da.Fill(ds)

Me.DataGrid1.DataSource = ds.Tables(0)

DataGrid1.SetDataBinding(ds, "tblLogged")



Me.DataGrid1.Refresh()

Dim member As String

' The name of a DataTable is Customers.

member = "Customers"

'Me.DataGrid1.FirstVisibleColumn(1)

'hide the first column

'log_ID

'ts.MappingName = DataGrid1.DataMember

'ts.MappingName = DataGrid1.DataSource

' Add it to the datagrid's TableStyles collection

'DataGrid1.TableStyles.Add(ts)

DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0

'' Set the DataGridTableStyle.MappingName property

'' to the table in the data source to map to.

'ts.MappingName = DataGrid1.DataMember

'' Add it to the datagrid's TableStyles collection

'DataGrid1.TableStyles.Add(ts)

'' Hide the first column (index 0)

'DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0







Me.DataGrid1.Refresh()

cn.Close()

End Sub
 
OK, so i figured it out myself (kindof, thanks for the head start Ken).

If anybody wants the code to hide the column read the following:



'under forms declarations add this

Inherits System.Windows.Forms.Form



Private Sub Query_Grid()

Dim strSQL As String

strSQL = "SELECT tblLogged.log_ID, format(tblLogged.time_start,'HH:MM') AS Start, format(tblLogged.time_end,'HH:MM') AS [End], tblLogged.act_cat AS Category, tblLogged.act_desc AS Description, tblLogged.user_name, tblLogged.act_date"

strSQL = strSQL & " FROM(tblLogged)"

strSQL = strSQL & " WHERE(((tblLogged.user_name) = '" & g_User & "') And ((tblLogged.act_date) = #" & String.Format("{0:MM\/dd\/yyyy}", Me.dtDate_Val.Value) & "#))"

strSQL = strSQL & " ORDER BY tblLogged.time_start, tblLogged.time_end;"



Dim AppPath As String = g_ODBC

Dim cn As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source = " + AppPath)

Dim cmd As New OleDbCommand(strSQL, cn)

Dim da As New OleDbDataAdapter(cmd)

Dim ds As New DataSet

da.Fill(ds)

DataGrid1.DataSource = Nothing

DataGrid1.DataSource = ds.Tables(0)

DataGrid1.SetDataBinding(ds, ds.Tables(0).ToString)

ts.MappingName = DataGrid1.DataMember

' Add it to the datagrid's TableStyles collection

DataGrid1.TableStyles.Add(ts)

DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0

'hide the last 2 columns

DataGrid1.TableStyles(0).GridColumnStyles(5).Width = 0

DataGrid1.TableStyles(0).GridColumnStyles(6).Width = 0

Me.DataGrid1.Refresh()

cn.Close()

End Sub
 
Save your fingers:

ds.Tables("dt").Columns("UnwantedColumn").ColumnMapping = MappingType.Hidden
DataGrid1.DataSource = ds.Tables("dt")


OK, so i figured it out myself (kindof, thanks for the head start Ken).
If anybody wants the code to hide the column read the following:

'under forms declarations add this
Inherits System.Windows.Forms.Form

Private Sub Query_Grid()
Dim strSQL As String
strSQL = "SELECT tblLogged.log_ID, format(tblLogged.time_start,'HH:MM') AS
Start, format(tblLogged.time_end,'HH:MM') AS [End], tblLogged.act_cat AS
Category, tblLogged.act_desc AS Description, tblLogged.user_name,
tblLogged.act_date"
strSQL = strSQL & " FROM(tblLogged)"
strSQL = strSQL & " WHERE(((tblLogged.user_name) = '" & g_User & "') And
((tblLogged.act_date) = #" & String.Format("{0:MM\/dd\/yyyy}",
Me.dtDate_Val.Value) & "#))"
strSQL = strSQL & " ORDER BY tblLogged.time_start, tblLogged.time_end;"

Dim AppPath As String = g_ODBC
Dim cn As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source
= " + AppPath)
Dim cmd As New OleDbCommand(strSQL, cn)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
DataGrid1.DataSource = Nothing
DataGrid1.DataSource = ds.Tables(0)
DataGrid1.SetDataBinding(ds, ds.Tables(0).ToString)
ts.MappingName = DataGrid1.DataMember
' Add it to the datagrid's TableStyles collection
DataGrid1.TableStyles.Add(ts)
DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0
'hide the last 2 columns
DataGrid1.TableStyles(0).GridColumnStyles(5).Width = 0
DataGrid1.TableStyles(0).GridColumnStyles(6).Width = 0
Me.DataGrid1.Refresh()
cn.Close()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top