Get data from each row of a column in C #

Posted on

Question :

It’s as follows, I have a DataGridView with multiple columns. In each line of this DataGridView will be presented the installation path of a program. With this displayed value, I’d like to use it to get the size of that folder and put it in a new column.

I have a method to calculate the size of a string:

private static long ObterTamanhoDiretorio(string tamanhoDir)
    DirectoryInfo dire = new DirectoryInfo(tamanhoDir.ToString());
    return dire.EnumerateFiles("*.*", SearchOption.AllDirectories).Sum(fi => fi.Length);

I do not know if it’s something like this that I have to use.

foreach (DataGridViewRow item in dataGridView1.Rows)


What I like to know is how to get this value by seeing each line.
If you have any ideas, thank you.


Answer :

If you wish to perform this action when loading the data you can use the event

  gdvView.DataSource = Lista;

protected void GdvViewOnRowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Cells[0].Text = "dados coluna 1";
            e.Row.Cells[1].Text = "dados coluna 2";

If you need to make the change after the Grid is populated, the for or foreach can be used.

  for (int i = 0; i < gdvView.Rows.Count; i++)
            gdvView.Rows[i].Cells[0].Text = "novos dados";


 foreach (var row in gdvView.Rows)
            ((gdvView)row).Cells[0].Text = "testeeeee";


Leave a Reply

Your email address will not be published. Required fields are marked *