R
Ron L
I have a dataset whose source is a SQL 2k stored procedure that I am trying
to display in a datagrid. This datasource has 4 columns that I am
interested in here, a text column and 3 value columns corresponding to
permissions to certain data classes. I want to put the permission values in
combo boxes in the grid and instead of displaying the numeric values, have
the combo box display a string that corresponds to the numeric value (i.e.
No Access for 0, Read for 1, Create for 3, and Edit for 7). I have written
the code below to try to implement this, but it throws an error on
rendering. The error is:
An unhandled exception of type 'System.InvalidCastException'
occurred in system.windows.forms.dll
Additional information: Specified cast is not valid.
I suspect, but can't be sure, that the cast error has to do with the binding
of the dataset to the pulldown. The column names that I am trying to bind
to are "TSL", "HCT", and "MCT"
Can anyone tell me what I am doing wrong here?
TIA,
Ron L
The code:
' Setup the permissions grid
' Setup the list of Display/Value pairs for the combo
boxes
Dim permsList As DataTable
permsList = New DataTable
permsList.Columns.Add(New DataColumn("Display", GetType(String)))
permsList.Columns.Add(New DataColumn("Id", GetType(Integer)))
permsList.Rows.Add(permsList.NewRow())
permsList.Rows.Add(permsList.NewRow())
permsList.Rows.Add(permsList.NewRow())
permsList.Rows.Add(permsList.NewRow())
permsList.Rows(0)(0) = "No Access"
permsList.Rows(0)(1) = 0
permsList.Rows(1)(0) = "Read"
permsList.Rows(1)(1) = 1
permsList.Rows(2)(0) = "Create"
permsList.Rows(2)(1) = 3
permsList.Rows(3)(0) = "Edit"
permsList.Rows(3)(1) = 7
' Fill the DataTable from the user data dataset (includes user
permissions)
Dim dt As DataTable = dsUserData.Tables(0)
Dim tableStyle As New DataGridTableStyle
tableStyle.GridColumnStyles.Clear()
tableStyle.MappingName = "Table"
tableStyle.MappingName = dt.TableName.ToString
' Setup the Subsystems column
Dim TextCol As New DataGridTextBoxColumn
TextCol.MappingName = dt.Columns(15).ColumnName
TextCol.HeaderText = dt.Columns(15).ColumnName
tableStyle.GridColumnStyles.Add(TextCol)
' Setup the permissions columns
Dim i As Integer
For i = 2 To 4
'Dim textcol2 As New DataGridTextBoxColumn
'textcol2.MappingName = dt.Columns(i).ColumnName
'textcol2.HeaderText = dt.Columns(i).ColumnName
'tableStyle.GridColumnStyles.Add(textcol2)
Dim ComboTextCol As New DataGridComboBoxColumn
ComboTextCol.MappingName = dt.Columns(i).ColumnName
ComboTextCol.HeaderText = dt.Columns(i).ColumnName
ComboTextCol.Width = 120
ComboTextCol.ColumnComboBox.Items.Clear()
ComboTextCol.ColumnComboBox.DataSource = permsList
ComboTextCol.ColumnComboBox.DisplayMember = "Display"
ComboTextCol.ColumnComboBox.ValueMember =
dt.Columns(i).ColumnName
' for the above line I have also tried setting it equal to "Id"
tableStyle.PreferredRowHeight =
ComboTextCol.ColumnComboBox.Height + 2
tableStyle.GridColumnStyles.Add(ComboTextCol)
Next
grdPermissions.TableStyles.Clear()
grdPermissions.TableStyles.Add(tableStyle)
grdPermissions.DataSource = dt
Dim grdStyle As DataGridTableStyle
'no adding of new rows thru dataview...
Dim cm As CurrencyManager
cm = CType(Me.BindingContext(grdPermissions.DataSource,
grdPermissions.DataMember), CurrencyManager)
CType(cm.List, DataView).AllowNew = False
to display in a datagrid. This datasource has 4 columns that I am
interested in here, a text column and 3 value columns corresponding to
permissions to certain data classes. I want to put the permission values in
combo boxes in the grid and instead of displaying the numeric values, have
the combo box display a string that corresponds to the numeric value (i.e.
No Access for 0, Read for 1, Create for 3, and Edit for 7). I have written
the code below to try to implement this, but it throws an error on
rendering. The error is:
An unhandled exception of type 'System.InvalidCastException'
occurred in system.windows.forms.dll
Additional information: Specified cast is not valid.
I suspect, but can't be sure, that the cast error has to do with the binding
of the dataset to the pulldown. The column names that I am trying to bind
to are "TSL", "HCT", and "MCT"
Can anyone tell me what I am doing wrong here?
TIA,
Ron L
The code:
' Setup the permissions grid
' Setup the list of Display/Value pairs for the combo
boxes
Dim permsList As DataTable
permsList = New DataTable
permsList.Columns.Add(New DataColumn("Display", GetType(String)))
permsList.Columns.Add(New DataColumn("Id", GetType(Integer)))
permsList.Rows.Add(permsList.NewRow())
permsList.Rows.Add(permsList.NewRow())
permsList.Rows.Add(permsList.NewRow())
permsList.Rows.Add(permsList.NewRow())
permsList.Rows(0)(0) = "No Access"
permsList.Rows(0)(1) = 0
permsList.Rows(1)(0) = "Read"
permsList.Rows(1)(1) = 1
permsList.Rows(2)(0) = "Create"
permsList.Rows(2)(1) = 3
permsList.Rows(3)(0) = "Edit"
permsList.Rows(3)(1) = 7
' Fill the DataTable from the user data dataset (includes user
permissions)
Dim dt As DataTable = dsUserData.Tables(0)
Dim tableStyle As New DataGridTableStyle
tableStyle.GridColumnStyles.Clear()
tableStyle.MappingName = "Table"
tableStyle.MappingName = dt.TableName.ToString
' Setup the Subsystems column
Dim TextCol As New DataGridTextBoxColumn
TextCol.MappingName = dt.Columns(15).ColumnName
TextCol.HeaderText = dt.Columns(15).ColumnName
tableStyle.GridColumnStyles.Add(TextCol)
' Setup the permissions columns
Dim i As Integer
For i = 2 To 4
'Dim textcol2 As New DataGridTextBoxColumn
'textcol2.MappingName = dt.Columns(i).ColumnName
'textcol2.HeaderText = dt.Columns(i).ColumnName
'tableStyle.GridColumnStyles.Add(textcol2)
Dim ComboTextCol As New DataGridComboBoxColumn
ComboTextCol.MappingName = dt.Columns(i).ColumnName
ComboTextCol.HeaderText = dt.Columns(i).ColumnName
ComboTextCol.Width = 120
ComboTextCol.ColumnComboBox.Items.Clear()
ComboTextCol.ColumnComboBox.DataSource = permsList
ComboTextCol.ColumnComboBox.DisplayMember = "Display"
ComboTextCol.ColumnComboBox.ValueMember =
dt.Columns(i).ColumnName
' for the above line I have also tried setting it equal to "Id"
tableStyle.PreferredRowHeight =
ComboTextCol.ColumnComboBox.Height + 2
tableStyle.GridColumnStyles.Add(ComboTextCol)
Next
grdPermissions.TableStyles.Clear()
grdPermissions.TableStyles.Add(tableStyle)
grdPermissions.DataSource = dt
Dim grdStyle As DataGridTableStyle
'no adding of new rows thru dataview...
Dim cm As CurrencyManager
cm = CType(Me.BindingContext(grdPermissions.DataSource,
grdPermissions.DataMember), CurrencyManager)
CType(cm.List, DataView).AllowNew = False