Dear Reader,
I have a DataGridView control on my form and when it's shown, the following
error is shown:
"System.ArgumentException: DataGridViewComboBoxCell value is not valid."
The DataGridView's datasource is set using the following code:
************************************************************
dataGridViewPersons.DataSource = businessLayer.GetListOfPersons(initialise,
companyID);
if (dataGridViewPersons.DataSource != null)
{
dataGridViewPersons.Columns["ID"].Visible = false;
dataGridViewPersons.Columns["ID_Company"].Visible = false;
}
public BindingSource GetListOfPersons(bool initialise, string companyID)
{
SqlCommand sqlCommand;
DataColumn dataColumn;
try
{
if (initialise)
{
sqlCommand = new SqlCommand();
sqlCommand.CommandText = "SELECT * FROM tblPersons";
sqlCommand.Connection = sqlConnection;
dataSetPersons.Clear();
sqlDataAdapterPersons = new SqlDataAdapter(sqlCommand);
sqlCommandBuilderPersons = new
SqlCommandBuilder(sqlDataAdapterPersons);
sqlDataAdapterPersons.Fill(dataSetPersons, "Persons");
dataColumn = new DataColumn("FullName");
dataColumn.Expression = "IIF( Len([FirstName]) >= 1,
TRIM([FirstName]) + ' ', '') + " +
"IIF( Len([MiddleName]) >= 1,
TRIM([MiddleName]) + ' ', '') + " +
"IIF( Len([LastName]) >= 1, [LastName],
'')";
dataSetPersons.Tables["Persons"].Columns.Add(dataColumn);
bindingSourcePersons = new BindingSource();
bindingSourcePersons.DataSource =
dataSetPersons.Tables["Persons"].DefaultView;
}
else
{
bindingSourcePersons.Filter = "ID_Company = " + companyID;
}
return (bindingSourcePersons);
}
catch (Exception exception)
{
errors.Add(exception.Message);
}
return (null);
}
************************************************************
To the datagrid a colums is added:
************************************************************
cmbGender = new System.Windows.Forms.DataGridViewComboBoxColumn();
cmbGender.DataPropertyName = "Gender";
cmbGender.HeaderText = "Gender";
cmbGender.Name = "cmbGender";
cmbGender.Width = 100;
cmbGender.Items.AddRange( new string[] {"Male", "Female"} );
dataGridViewPersons.Columns.Add(cmbGender);
************************************************************
Question: How can I solve this error???
Thank you!
Johan Machielse
Ordina SI&D - DCM
Bart Mermuys - 28 Sep 2006 21:03 GMT
Hi,
> Dear Reader,
>
[quoted text clipped - 71 lines]
> cmbGender.Items.AddRange( new string[] {"Male", "Female"} );
> dataGridViewPersons.Columns.Add(cmbGender);
Are you sure the Gender column only contains the (exact) string values
"Male" or "Female"? If there are any other values you'll get such an
Exception.
HTH,
Greetings
> ************************************************************
> Question: How can I solve this error???
[quoted text clipped - 3 lines]
> Johan Machielse
> Ordina SI&D - DCM