I am working on adding a custom radio button column to a databound datagridview in a VB.Net app (Visual Studio 2005) and I'm a bit out of my depth. To get access to the custom classes, I've added a reference to a dll offered by Microsoft at
http://msdn2.microsoft.com/en-us/library/aa730882(VS.80).aspx
When I bind my datagridview to my dataset, the datagridview has the custom column and each cell in the column contains an unchecked radio button and the text false. What I need help with are two things:
1. When the datagridview is bound to its datasource to make one cell in the column have its radio button checked and its text reading "true"
2. A way to allow a user to click the radio button in any cell and for the radio button's check state to change (at the moment when I click a radio button this exception is thrown: "£System.ArgumentException: Formatted value of the cell has a wrong type" and indeed when I tested for the FormattedValue and Value properties of my custom column, I saw that both properties returned nothing.
Here's my code as I guess that will help:
Try
aLoading = True
'set values for controls
Me.Text = "Change address for " & cRow("Contact")
Me.optMoveAddress.Text = "Move " & cRow("Contact") & " to another address at " & cRow("OrganisationName")
Me.optNewAddress.Text = "Add new address for " & cRow("OrganisationName") & " and move " & cRow("Contact") & " to this address."
Me.optMoveAddress.Checked = True
Org = cRow("OrganisationID")
'get addresses for organisation
frmData.Ds1.spContactAddressesByOrg.Columns("Current Address").ReadOnly = False 'set to false to enable contact's current address to be checked in dgAddresses
frmData.SpContactAddressesByOrgTableAdapter.Fill(frmData.Ds1.spContactAddressesByOrg, Org)
'find row in dataset for contact's address at this org
Dim CurrAddr As Integer = frmData.Ds1.spContactAddressesByOrg.Rows.Find(cRow("AddressID")).Item(0)
'Debug.WriteLine("CurrAddr: " & CurrAddr)
'make checkbox against contact's current address checked in datagrid
'For i As Integer = 0 To Me.dgAddresses.Rows.Count - 1
' If dgAddresses.Item("AddressID", i).Value = CurrAddr Then Me.dgAddresses.Item("Current Address", i).Value = True
'Next
With c
.Name = "RadioButton Column"
.HeaderText = "Select address"
.ReadOnly = False
.DataSource = frmData.Ds1.spContactAddressesByOrg
.DisplayMember = "Current Address"
'.ValueMember = "Current Address"
.ValueMember = "AddressID"
End With
With Me.dgAddresses
.DataSource = frmData.Ds1.spContactAddressesByOrg
'.Columns(0).Visible = False
'.Columns(1).Visible = False
'.Columns(8).Visible = 0
.Columns(10).ReadOnly = False 'set to false to enable contact's current address to be checked
.Columns(0).ReadOnly = False
.Columns.Add(c)
End With
For i As Integer = 0 To Me.dgAddresses.Rows.Count - 1
If dgAddresses.Item("AddressID", i).Value = CurrAddr Then
'Debug.WriteLine("dg: " & dgAddresses.Item("AddressID", i).Value)
'Debug.WriteLine(Me.dgAddresses.Item(0, i).Value.ToString)
'Me.dgAddresses.Item(0, i).Value = True
Me.dgAddresses.Item(0, i).Selected = True
End If
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
aLoading = False
End Try
So any help much appreciated
Louise

Custom radio button column in DataGridView