none
Error on datagridview show the "format string can be only "G", "g", "X", "x", "F", "f", "D" or "d"." RRS feed

  • Question

  • I have datagridview when display data on datagridview i get error :

    The "Format String can be only "G", "g", "X", "x", "F", "f", "D" or "d"."



    i debug after that get stack trace that show more details about error as following :

    at System.Enum.ToString(String format)
      at System.Enum.ToString(String format, IFormatProvider provider)
      at System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue)
      at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
      at System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)






    How to solve this error please ?

    Tuesday, August 27, 2019 3:07 AM

All replies

  • Well, what format string are you trying to use?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, August 27, 2019 3:43 AM
  • Greetings Ahmed.

    That looks like you have a cell defined as numeric and you are trying to assign an enum to it. You need either to change the cell to a string value, or cast the enum to an int.

    enum testenum { zero, one, two, three };
    
    .
    .
    .
    
    dataGridView1.Rows[0].Cells[0].Value =(int) testenum.one; // Cast to int before assigning to the cell.

    Tuesday, August 27, 2019 3:43 AM