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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I hide a column value from datagrid?

Status
Not open for further replies.

lydro

Programmer
Mar 7, 2005
36
CA
I'm binding the data to datagrid in vb.net, and the first column is the entity_id, I don't want it to show in the datagrid, but I need to pass the entity_id to another window when the current row is selected. The entity_id is showing in my first column in my datagrid, how can I get the entity_id without showing the entity_id?

Thaks


 
do you need to have the value in a click or where exactley do you need it?

 
DataGridColumn.Visible Property
Visual Basic
Public Property Visible As Boolean

Property Value
true if the column is visible in the DataGrid control; otherwise, false. The default value is true.

Remarks
Use the Visible property to programmatically control whether the column is visible in the DataGrid control.




- free online Compare/Diff of snippets
 
for example..here is my datagrid result shows:
Entity_ID Name phone# address
10 XX 333 XXX
11 XX 444 XXX

I don't want the 10, 11 value to be showed in the first column, but when I click the first row, the entity_id vaule 10 shows in the popup window.
 
If I want the column(0) to be visible, how do I need to exactual code it? I can't find the DataGridColumn.visible property in the datagrid.

Could you please give more detail and example coding for it.

Thank you very much!
 
I need to pass the entity_id to another window to be able to show more detail of the data. In the other window, you can edit or detele the data.
 
John, I'm so sorry that I need to ask you for a very simple question. when I have your code datagrid1.Columns(0).Visible = False in my form, it shows error "Columns is not a memeber of 'System.Windows.Forms.Datagrid'".
 
Use intellisence. I don't use datagrids, so I can't say off the top of my head, but type the name of your datagrid object and then a period (.) A list of properties and methods should come up. scroll through that list and look for anything that sounds column like. (col, column, cols, columns, colvisible, etc) hover the mouse over the item in that list and a short description will pop up.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Thanks, guys.

But I tried anything I can. it just can't fix the problem I have now. I tried to hide the column, then it won't show in the datagrid, but it couldn't pass the entity_id value. if I don't hide the column, then I can pass the value. I can't do either of them. I want it to work in both ways.
 
There may well be, or at least there should be a simpler solution than I will show here, but after struggling with this same issue the other day, I found that the following will at least work.

When searching the help index in VB.Net it indicates that you should use the "RemoveAt" method of the "GridColumnStylesCollection" object. Personally, I could never get this to work. Also, I could never find the "DataGridColumn.Visible" option. (I think this was a Visual Basic 6.0 feature that did not make the transition. Not sure, but that is my suspicion.)

Anyway, I was able to accomplish my objective (of having a hidden column from which I could still obtain data) by doing the following:

1. Programmatically build the datatable which will create the columns for my datagrid. I chose to declare a private modular variable of "_dtMyTable". (You can accomplish this same objective in a couple of ways.) I'll use your example for clarity which you may be able to copy and paste to get you started.

Private _dtMyTable as New DataTable(TableName)

Private Sub BuildMyTable()

'this procedure will build a temporary datatable

With _dtMyTable.Columns
.Add("Entity_ID", Type.GetType("System.Integer")).ColumnMapping = MappingType.Hidden '(Note: This will hide your column from view but it will still be accessible from the dataview that we will create later)
.Add("Date Of Order", Type.GetType("System.DateTime"))
.Add("Name", Type.GetType("System.String"))
.Add("Phone #", Type.GetType("System.String"))
.Add("Address", Type.GetType("System.String"))
End With

End Sub


Now you need to fill the blank table with data from your dataset(assuming here that you have already performed the necessary DataAdapter.Fill(MyDataSet,"TableName")procedure.)

Private Sub LoadMyTable()
Dim dr As DataRow
Dim dr2 As DataRow

'this procedure will add the rows to the temp table
'Check to make sure that the table has been built
If _dtMyTable.Columns.Count = 0 Then
Call BuildMyTable()
End If

'This example assumes (I know I shouldn't do that, but...)that the column names in your database / dataset are the same as the column names of the table we just created.
For Each dr2 In MyDataSet.Tables(0).Rows
dr = _dtMyTable.NewRow
dr("Entity_ID") = dr2("Entity_ID")
dr("Name") = dr2("Name")
dr("Phone #") = dr2("Phone #")
dr("Address") = dr2("Address")

_dtMyTable.Rows.Add(dr)
Next

'now bind a private dataview to the filled table
(Remember you must have created the private Dataview "dvMyDataView" previously, either in design view or programmatically)
dvMyDataView.Table = _dtMyTable '(Note: If you have assigned this value in design view it will be overridden here.)
dgMyDataGrid.DataSource = _dvMyDataView

End Sub

Now, when you want to retrieve that data, you must retrieve it from _dvMyDataView and not from the DataGrid. The DataView will still hold all of the data, it is just not visible in the datagrid.

I know this is a long way around getting this, so seemingly simple task accomplished, but it is the only way that I could get this to work for me. If there is a better / simpler solution out there, please let me know as I will be needing to use this in several instances in my current project.



 
Here's a few other ideas for you.

How are you populating the grid? If it's coming from a data source, why not just get the value you need from it?

Or, even though the column is hidden, the value is still there. You may have to specify the cell explicitly, but it still exists.

Or, the cheap way, just set the column's width to 0.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
THANKS ALL YOUR HELP, GUYS!

I fixed the problem, I set the column width to 0 in dataGridTextBoxColumn..
 
Mind showing the code so we don't end up looking for a property that doesn't exist. I've solved this little issue myself by calling my function SetGridTableColumnStyles but would like to know any shortcut that may be beneficial.


Private Sub SetGridTableColumnStyles()

Dim ts1 As New DataGridTableStyle

' Set the mapping name to the datasource
ts1.MappingName = "ReportData"

' Set other properties.
ts1.AlternatingBackColor = Color.LightGray
' Add a GridColumnStyle and set its MappingName
' to the name of a DataColumn in the DataTable.
' Set the HeaderText and Width properties.

' Add the first column style.
Dim TextCol1 As New DataGridTextBoxColumn
TextCol1.MappingName = "ProjID"
TextCol1.Width = 0
ts1.GridColumnStyles.Add(TextCol1)

---------------------
scking@arinc.com
---------------------
 
BTW, if you don't want to show that column in the grid, why do you want to add it the grid?

Instead of setting the DataGridTextBoxColumn width to 0, I would avoid adding that GridColumnStyles.

-Kris
 
dsKlanten.Tables("klanten").Columns("Id").ColumnMapping = MappingType.Hidden

Eric De Decker
eric.de.decker@pandora.be

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top