简体   繁体   中英

c# DataGridView get Cells Value from Selected Row

how do I made the proper syntax of this foreach loop. I want to get every cell value from row selected. I already set the selection mode to FullRowSelect

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
      foreach (//datagrid cell in current_selected_row) <--- proper Syntax condition
      {
           if(cell != null)
           {
                MessageBox.Show(cell.Value.ToString());
           }
      }
}

You can find current row this way:

var row = this.dataGridView1.CurrentRow;

Then having current row you can find cell values in one of these ways

row.Cells.Cast<DataGridViewCell>()
   .ToList()
   .ForEach(cell =>
   {
       MessageBox.Show(string.Format("{0}", cell.Value));
   });

That is equivalent to :

foreach (DataGridViewCell cell in row.Cells)
{
    MessageBox.Show(string.Format("{0}", cell.Value));
}

Or this way

for (int i = 0; i < row.Cells.Count; i++)
{
    MessageBox.Show(string.Format("{0}", row.Cells[i].Value));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM