简体   繁体   English

c#DataGridView从选定的行获取单元格值

[英]c# DataGridView get Cells Value from Selected Row

how do I made the proper syntax of this foreach loop. 我如何使此foreach循环正确的语法。 I want to get every cell value from row selected. 我想从选定的行中获取每个单元格值。 I already set the selection mode to FullRowSelect 我已经将选择模式设置为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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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