DataGridView changer le type des celulles

Poulpy87 Messages postés 14 Date d'inscription samedi 15 mars 2008 Statut Membre Dernière intervention 9 mars 2009 - 15 nov. 2008 à 23:11
Poulpy87 Messages postés 14 Date d'inscription samedi 15 mars 2008 Statut Membre Dernière intervention 9 mars 2009 - 17 nov. 2008 à 10:52
Bonsoir, je souhaiterais changer le type des cellules contenue dans
une DataGridView après l'avoir chargé avec une data table. Or lorsque j'essaie je reçois une erreur du type :

---------------------------
Boîte de dialogue Erreur par défaut DataGridView
---------------------------
L'exception suivante s'est produite dans le DataGridView :

System.FormatException: Cast non valide de 'System.String' en 'System.Drawing.Image'. ---> System.InvalidCastException: Cast non valide de 'System.String' en 'System.Drawing.Image'.

   à System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)

   à System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)

   à System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)

   à System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)

   --- Fin de la trace de la pile d'exception interne ---

   à System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)

   à System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue)

   à System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)

   à System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)

Voici mon code :

private void ChargerImage()
        {
            foreach (DataGridViewRow row in dgGestionCommerciale2.Rows)
            {
                foreach (DataGridViewColumn col in dgGestionCommerciale2.Columns)
                {
                   
                    switch (dgGestionCommerciale2[col.Index, row.Index].Value.ToString())
                    { 
                        case "1" :
                            dgGestionCommerciale2[col.Index, row.Index].Value = Image.FromFile(ConfigurationManager.AppSettings["imageIndicVert"]);
                            break;
                        case "2" :
                            dgGestionCommerciale2[col.Index, row.Index] = new DataGridViewImageCell();
                            dgGestionCommerciale2[col.Index, row.Index].Value = Image.FromFile(ConfigurationManager.AppSettings["imageIndicJaune"]);
                            break;
                        case "3" :
                            dgGestionCommerciale2[col.Index, row.Index] = new DataGridViewImageCell();
                            dgGestionCommerciale2[col.Index, row.Index].Value = Image.FromFile(ConfigurationManager.AppSettings["imageIndicRouge"]);
                            break;

                        default:
                            break;
                    }
                   
                }  
            }
        }

Merci d'avance !

4 réponses

bubbathemaster Messages postés 339 Date d'inscription dimanche 26 janvier 2003 Statut Membre Dernière intervention 25 mars 2009 4
16 nov. 2008 à 02:50
switch (dgGestionCommerciale2[col.Index, row.Index].Value.ToString())
                    { 
                        case "1" :
                           
dgGestionCommerciale2[col.Index, row.Index].Value =
Image.FromFile(ConfigurationManager.AppSettings["imageIndicVert"]);
                            break;

là t'essaye de charger une image dans une DataGridViewTextBoxCell. Forcemment, ca coince. Le remplissage automatique de datagridview a ses limites. Le plus simple c'est de parcourir ton dataset et de remplir la dgv avec:
dgv.Rows.Add("toto", 12.0f, Image.FromFile("c:\\toto.bmp"));
0
bubbathemaster Messages postés 339 Date d'inscription dimanche 26 janvier 2003 Statut Membre Dernière intervention 25 mars 2009 4
16 nov. 2008 à 02:52
Ha oui j'oubliais, utilise plutot l'evenement "CellFormatting" d'une datagridview plutot qu'une fonction que tu appelle et qui parcours tout le bordel, c'est plus propre :)
0
Poulpy87 Messages postés 14 Date d'inscription samedi 15 mars 2008 Statut Membre Dernière intervention 9 mars 2009
16 nov. 2008 à 18:08
Merci je vais essayer !
0
Poulpy87 Messages postés 14 Date d'inscription samedi 15 mars 2008 Statut Membre Dernière intervention 9 mars 2009
17 nov. 2008 à 10:52
Alors voila j'ai fais de cette façon, je n'ai plus d'erreur mais a lieu de m'afficher mon image il m'affiche System.Drawing.Bitmap.
Qu'ai je mal fais ?

private void dgGestionCommerciale2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex >= 1)
{
switch (e.Value.ToString())
{
case "1":
dgGestionCommerciale2[e.ColumnIndex, e.RowIndex].ValueType = typeof(System.Drawing.Image);
dgGestionCommerciale2[e.ColumnIndex, e.RowIndex].Value = Image.FromFile(ConfigurationManager.AppSettings["imageIndicVert"]);
break;
case "2":
dgGestionCommerciale2[e.ColumnIndex, e.RowIndex].ValueType = typeof(System.Drawing.Image);
dgGestionCommerciale2[e.ColumnIndex, e.RowIndex].Value = Image.FromFile(ConfigurationManager.AppSettings["imageIndicJaune"]);
break;
case "3":
dgGestionCommerciale2[e.ColumnIndex, e.RowIndex].ValueType = typeof(System.Drawing.Image);
dgGestionCommerciale2[e.ColumnIndex, e.RowIndex].Value = Image.FromFile(ConfigurationManager.AppSettings["imageIndicRouge"]);
break;

default:
break;
}
}
0
Rejoignez-nous