c# - 无法读取 GridView 中行的前两列

标签 c# asp.net vb.net

我需要读取 GridView 中一行的所有列。我使用以下代码来执行此操作。

      foreach (GridViewRow row in gridvw.Rows)
      {
          DataRow dr;
          dr = table.NewRow();

          //string str = gridvw.Rows(0).Cells(0).Text;
          for (int i =0; i < row.Cells.Count-1; i++)
          {
              dr[i] = row.Cells[i].Text.Replace("& ", "");
          }
          table.Rows.Add(dr);
      }

正在将所有值添加到表中。上面的代码对于每行的前 2 列不返回任何内容。

有人可以帮忙解决这个问题吗?

提前致谢,

约瑟夫

最佳答案

试试这个

       DataGridViewRowCollection rowCollection = dataGridView1.Rows;
        label1.Text = "";
        foreach (DataGridViewRow item in rowCollection)
        {
            string str = "";

            foreach (DataColumn col in dataTable.Columns)
            {
                str += item.Cells[col.ToString()].Value + "  ";

            }
            label1.Text += "\n" + str;
        }

关于c# - 无法读取 GridView 中行的前两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18892170/

相关文章:

c# - 如何将两个不同的字符串合并为一个字符串?

c# - 在asp中动态添加控件时更改母版页中布局的高度

vb.net - Visual Basic资源

vb.net - 如何在 VB.Net 中使用 Adob​​e Indesign API

c# - 根据 asp GridView 列中的值显示图像

vb.net - 如何在Visual Basic中检查单个组件。错误检查

c# - Paypal 快速结帐和 ASP.NET

c# - 与抽象类的接口(interface)

c# - 如何获取 webjob 内 azure 托管的 asp .net core 应用程序的 wwwroot 文件夹中图像文件的路径

asp.net - Web 窗体错误消息 : "This is not scriptlet. Will be output as plain text"