Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / September 2006

Tip: Looking for answers? Try searching our database.

Add DataGridViewComboBoxColumn to DataGridView Error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johan Machielse - 28 Sep 2006 15:31 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.