Count the number of characters of a value within a column of a Datagridview [C#]

Asked

Viewed 75 times

0

How to count the number of characters of a value within a column of a DataGridView?

The goal is to add €0.02 for each letter in the column mensagem and then put the total in a Label.

inserir a descrição da imagem aqu

  • 2

    If we are on [pt.so], why not ask in Portuguese yourself?

1 answer

1

The answer is relatively easy with LINQ:

var linhas = myDataGridView.Rows.Cast<DataGridViewRow>().Select(r => Convert.ToString(r.Cells["mensagem"].Value));
double somaLinhas = linhas.Sum(r => r.Length * 0.02);

lblSoma.Text = string.Format(@"{0:#,##0.00}", somaLinhas);

The name of the column mensagem must be amended if that name is not effective, and Label lblSoma and the myDataGridView.

Browser other questions tagged

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