【问题标题】:How to loop through each and every row, column and cells in a GridView and get its value如何遍历 GridView 中的每一行、每一列和每一单元格并获取其值
【发布时间】:2012-09-01 12:31:37
【问题描述】:

我有一个GridView,它是数据绑定的,在 button_click 上我想逐行读取GridView 每列并读取一行中每个单元格的值并更新数据库中的表?我也知道如何检查该单元格是否包含 null。

我正在尝试这样的事情并卡住了:

protected void SAVE_GRID_Click(object sender, EventArgs e)
{
    int rowscount = GridView2.Rows.Count;
    int columnscount = GridView2.Columns.Count;
    for (int i = 0; i < rowscount; i++)
    {
        for (int j = 1; j < columnscount; j++)
        {
            // I want to get data of each cell in a row
            // I want to read the corresponding header and store
        }
    }       
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    最简单的方法是使用foreach

    foreach(GridViewRow row in GridView2.Rows)
    {
        // here you'll get all rows with RowType=DataRow
        // others like Header are omitted in a foreach
    }
    

    编辑:根据您的编辑,您访问的列不正确,您应该从 0 开始:

    foreach(GridViewRow row in GridView2.Rows)
    {
        for(int i = 0; i < GridView2.Columns.Count; i++)
        {
            String header = GridView2.Columns[i].HeaderText;
            String cellText = row.Cells[i].Text;
        }
    }
    

    【讨论】:

    • @ArchanaB.R:编辑了我的答案。但这仅在您使用 BoundFields 而不是 TemplateFields 时才有效。然后你需要在行上使用FindControl 来获取对你的控件的引用。
    • 是的,我正在使用 BoundFields。嗯......但我想从第 2 栏阅读。所以我可以给 for(int i = 1; i
    • @ArchanaB.R:当然,如果你愿意,你可以从 1 开始。所以如果你使用row.Cells[i].Text,它应该可以工作,如上所示。
    • @ArchanaB.R:您可以使用int.Parse(cellText) 将字符串解析为int。
    • 我用过:int cellText = Convert.ToInt16(row.Cells[i].Text);对吗?
    【解决方案2】:

    正如“Tim Schmelter”所说,这是最好的方法:只需将其代码更改为 seconde 循环( GridView2.Columns[i].Count by row.Cells.Count ),看起来就是这样:

    foreach(GridViewRow row in GridView2.Rows)
    {
        for(int i = 0; i < GridView2.Columns.Count; i++)
        {
            String header = GridView2.Columns[i].HeaderText;
            String cellText = row.Cells[i].Text;
        }
    }
    

    谢谢。

    【讨论】:

      【解决方案3】:
      foreach (DataGridViewRow row in GridView2.Rows)
                  {
                      if ( ! row.IsNewRow)
                      {
                          for (int i = 0; i < GridView2.Columns.Count; i++)
                          {
                              String header = GridView2.Columns[i].HeaderText;
                              String cellText = Convert.ToString(row.Cells[i].Value);
                          }
                      }
                  }
      

      此处在迭代单元格值之前需要检查 NewRow。

      【讨论】:

        【解决方案4】:
        foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    for (int i = 0; i < dataGridView1.Columns.Count; i++)
                    {
                        String header = dataGridView1.Columns[i].HeaderText;
                        //String cellText = row.Cells[i].Text;
                        DataGridViewColumn column = dataGridView1.Columns[i]; // column[1] selects the required column 
                        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; // sets the AutoSizeMode of column defined in previous line
                        int colWidth = column.Width; // store columns width after auto resize           
                        colWidth += 50; // add 30 pixels to what 'colWidth' already is
                        this.dataGridView1.Columns[i].Width = colWidth; // set the columns width to the value stored in 'colWidth'
                    }
                }
        

        【讨论】:

        • 请不要只发布代码 - 如果可能的话,还要解释它是如何解决问题的。
        猜你喜欢
        • 1970-01-01
        • 2018-08-26
        • 1970-01-01
        • 2021-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-17
        • 1970-01-01
        相关资源
        最近更新 更多