【问题标题】:Reading data from DataGridView in C#从 C# 中的 DataGridView 读取数据
【发布时间】:2011-06-27 01:23:52
【问题描述】:

如何在 C# 中从 DataGridView 读取数据?我要读取的数据出现在表中。如何浏览线路?

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    类似

    for (int rows = 0; rows < dataGrid.Rows.Count; rows++)
    {
         for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++)
        {
            string value = dataGrid.Rows[rows].Cells[col].Value.ToString();
    
        }
    } 
    

    不使用索引的例子

    foreach (DataGridViewRow row in dataGrid.Rows)
    { 
        foreach (DataGridViewCell cell in row.Cells)
        {
            string value = cell.Value.ToString();
    
        }
    }
    

    【讨论】:

    • 您可以简单地编写“var”,而不是分别编写行或单元格的类型,即“DataGridViewRow”或“DataGridViewCell”。
    • @kami 如果使用 var,row.Cells 会抛出错误,因为它认为 row 是类型对象
    【解决方案2】:

    如果您愿意,也可以使用列名代替列号。

    例如,如果您想从 DataGridView 中读取数据 4. 行和“名称”列。 它让我更好地理解我正在处理的变量。

    dataGridView.Rows[4].Cells["Name"].Value.ToString();
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:
      string[,] myGridData = new string[dataGridView1.Rows.Count,3];
      
      int i = 0;
      
      foreach(DataRow row in dataGridView1.Rows)
      
      {
      
          myGridData[i][0] = row.Cells[0].Value.ToString();
          myGridData[i][1] = row.Cells[1].Value.ToString();
          myGridData[i][2] = row.Cells[2].Value.ToString();
      
          i++;
      }
      

      希望这会有所帮助....

      【讨论】:

        【解决方案4】:

        代码示例:从 DataGridView 读取数据并将其存储在数组中

        int[,] n = new int[3, 19];
        for (int i = 0; i < (StartDataView.Rows.Count - 1); i++)
        {
            for (int j = 0; j < StartDataView.Columns.Count; j++)
            {
                if(this.StartDataView.Rows[i].Cells[j].Value.ToString() != string.Empty)
                {
                    try
                    {
                        n[i, j] = int.Parse(this.StartDataView.Rows[i].Cells[j].Value.ToString());
                    }
                    catch (Exception Ee)
                    { //get exception of "null"
                        MessageBox.Show(Ee.ToString());
                    }
                }
            }
        }
        

        【讨论】:

        • 我没有在你的例子中得到 try-catch。您应该在解析之前测试以确保单元格不为空。
        【解决方案5】:
         private void HighLightGridRows()
         {            
             Debugger.Launch();
             for (int i = 0; i < dtgvAppSettings.Rows.Count; i++)
             {
                 String key = dtgvAppSettings.Rows[i].Cells["Key"].Value.ToString();
                 if (key.ToLower().Contains("applicationpath") == true)
                 {
                     dtgvAppSettings.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                 }
             }
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-29
          • 1970-01-01
          • 2016-07-08
          • 2014-04-05
          • 1970-01-01
          相关资源
          最近更新 更多