Get data from each row of a column in C #

6

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.

    
asked by anonymous 05.06.2017 / 11:17

1 answer

0

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

  gdvView.DataSource = Lista;
  gdvView.DataBind();


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";
        }

Or

 foreach (var row in gdvView.Rows)
        {
            ((gdvView)row).Cells[0].Text = "testeeeee";
        }
    
05.06.2017 / 14:42