Foreach in Datatable

Asked

Viewed 107 times

4

Have a datatable and needed to make a foreach to change some values of this datatable

public static DataTable TabelaAmbientes { get; set; }

I was doing the following foreach:

foreach (DataRow lista in TabelaAmbientes.Rows)
                    {
                        switch (lista.CatRem_Tipo)
                        {
                            case "C":
                                lista.CatRem_Tipo = "Comissão/Participação";
                                break;

                            case "P":
                                lista.CatRem_Tipo = "Premiação";
                                break;

                            case "A":
                                lista.CatRem_Tipo = "Ambos";
                                break;
                         }
                      }

But in doing lista.CatRem_Tipo me of the error of

Datarow does not contain a definition for Catrem_type

What can I do?

  • 1

    lista["CatRem_Tipo"].Value ???

  • Where could I put this? on the switch not right

1 answer

4


Assuming CatRem_Tipo is the name of the column:

var dic = new Dictionary<string, string>
{
    { "C", "Comissão/Participação" },
    { "P", "Premiação" },
    { "A", "Ambos" }
};

foreach (DataRow dataRow in TabelaAmbientes.Rows)
{
    string value = dataRow["CatRem_Tipo"].ToString();

    if (dic.ContainsKey(value))
    {
       dataRow["CatRem_Tipo"] = dic[value];
    }
}

Edit: replacing the for for foreach

  • I will take some time to accept the answer, because I am no longer in the company, but tomorrow I test and I accept, if I solve my problem

  • Thank you for the reply

  • It worked, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.