I have the following code in place to size datacolumns according to the
data in the cell. It seems to be working fine, But I am having a hard
time hiding a column. I am not sure how to reference the column name. I
think I need to reference a columnstyle, or perhaps a tablestyle, or
maybe both. This is completely confusing me. If someone could educate
me on the process, I would be very thankful.

Some of the column names are as follows:
Activity_Activity_CD
Activity_Result_CD
Activity_Date
Activity_FollowUP

What I would like to do is hide or set the width of a column (Let's use
Activity_Date) to zero if radiobutton1 is checked.

Any ideas? Do you need more information or code?

Thanks,
Aaron


Look for ******* in the following code.


Function DataGridApplyAutomaticWidths _
(ByVal DataGrid1 As System.Windows.Forms.DataGrid, ByVal
NumberOfRowsToScan As Integer, ByVal MaxPixelWidth As Integer) _
As DataGridTableStyle
'Create graphics object for measuring widths.
Dim Graphics As Graphics = DataGrid1.CreateGraphics()
'Define new table style.
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle()
Try
Dim DataTable As DataTable
DataTable = DataGrid1.DataSource.DataSet.Tables(0)
'DataTable = DataGrid1.DataSource.Tables(0)
'Can only scan rows if they exist.
NumberOfRowsToScan = System.Math.Min(NumberOfRowsToScan,
DataTable.Rows.Count)
'Clear any existing table styles.
DataGrid1.TableStyles.Clear()
'Use mapping name that is defined in the data source.
TableStyle.MappingName = DataTable.TableName
'Now create the column styles within the table style.
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
For Each Column In DataTable.Columns
ColumnStyle = New DataGridTextBoxColumn()
With ColumnStyle
.TextBox.Enabled = True
.HeaderText = Column.ColumnName
.MappingName = Column.ColumnName
'Set width to header text width.
Width = Graphics.MeasureString(.HeaderText,
DataGrid1.Font, MaxPixelWidth).Width
End With
'Change width, if data width is wider than header text
width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim DataRow As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
DataRow = DataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width,
Graphics.MeasureString(DataRow(Column.ColumnName), DataGrid1.Font,
MaxPixelWidth).Width)
End If
Next
ColumnStyle.Width = Width + 4


'*******Here is the condition check

If RadioButton1.checked Then

'*******HERE IS WHERE I WANT TO SET THE WIDTH OF SPECIFIC COLUMNS TO ZERO
'*******(I THINK)

End If
'Add the new column style to the table style.
TableStyle.GridColumnStyles.Add(ColumnStyle)
Next
'Add the new table style to the data grid.
DataGrid1.TableStyles.Add(TableStyle)
Finally
Graphics.Dispose()
End Try
Return TableStyle
End Function