Dealy

Dealy

  • NA
  • 213
  • 0

System.FormatException:DataGridViewComboBoxCell Error

Apr 23 2014 3:57 AM
Hello,
I'm getting this error "System.FormatException:DataGridViewComboBoxCell value is not valid" even though I'm using the same data types. The error occures on line combo.DataPropertyName = code1 when I try to assign values from one dataGridViewColumn to DataGridViewComboBoxColumn .
 
Here's some details about my variables and the code:
sproc applies to a stored procedure that returns TaskIdentity(int), TaskDescription.
code = "TaskIdentity"
description = "TaskDescription"
code1= tasksGridView.Columns[1].HeaderText

public void fillGridViewComboColumn(SqlConnection conn, DataGridViewComboBoxColumn combo, String sproc, String code, String code1,String description)
{
using (conn = new SqlConnection(connectionString))
{
  using (SqlCommand cmd2 = new SqlCommand(sproc))
   {
    cmd2.CommandType = CommandType.StoredProcedure;
    conn.Open();
    cmd2.Connection = conn;

    using (SqlDataReader rdr2 = cmd2.ExecuteReader(CommandBehavior.CloseConnection))
     {

        DataTable dt2 = new DataTable();
        dt2.Columns.Add(code,
        typeof(decimal));
        dt2.Columns.Add(description, typeof(string));
        while (rdr2.Read())
         {
            DataRow dr2 = dt2.NewRow();
            dr2[0] = rdr2.GetInt32(rdr2.GetOrdinal(code));
            dr2[1] = rdr2.GetString(rdr2.GetOrdinal(description));
            dt2.Rows.Add(dr2);
         }

        rdr2.Close();
 
        combo.DataSource = dt2;
        combo.DisplayMember = description;
        combo.ValueMember = code;
        combo.DataPropertyName = code1;
     }
   }
  }
}
 
Can anyone help me?
 
Thank you in advance.

Answers (1)