Groups | Blog | Home
all groups > dotnet windows forms databinding > september 2006 >

dotnet windows forms databinding : Add DataGridViewComboBoxColumn to DataGridView Error


Johan Machielse
9/28/2006 7:31:02 AM
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
9/28/2006 8:03:50 PM
Hi,

"Johan Machielse" <JohanMachielse@discussions.microsoft.com> wrote in
message news:B8EFCAFC-BDE5-456C-8A1F-3A13ACC9AE6B@microsoft.com...
[quoted text, click to view]

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

[quoted text, click to view]

AddThis Social Bookmark Button