如何遍历datagridview中的每一行?

时间:2016-01-10 07:05:43

标签: c# datagridview

我试图制作"当前计数"我的DataGridView的列只接受整数。我使用

时取得了成功
int value;
string str = my_datagridview.Rows[1].Cells["Current Count"].Value.ToString();
if (int.TryParse(str, out value))

但我确信必须有一种方法,我不必为每一行编写代码。我已经用for循环尝试了它,它给了我一个NullReferenceExeption。

这是我的代码(包括循环的NullReferenceException):

//'Export data' button
    void export_button_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < my_datagridview.RowCount; i++)
        {

        int value;
        string str = my_datagridview.Rows[i].Cells["Current Count"].Value.ToString();
        if (int.TryParse(str, out value))
        {
            //clears form and ensures new file will have header
            text_box_export.Text = "Item Code,Item Description,Current Count,On Order\r\n";

            //ints for for loops
            int row_count = my_datagridview.RowCount;
            int cell_count = my_datagridview.Rows[0].Cells.Count;

            //captures data for each row
            for (int row_index = 0; row_index <= row_count - 2; row_index++)
            {
                //captures data for each column in each row
                for (int cell_index = 0; cell_index < cell_count; cell_index++)
                {
                    //adds data to messagebox in stocklist format & leaves comma off the end of each line
                    if (cell_index <= 2)
                    {
                        text_box_export.Text = text_box_export.Text + my_datagridview.Rows[row_index].Cells[cell_index].Value.ToString() + ",";
                    }
                    else
                    {
                        text_box_export.Text = text_box_export.Text + my_datagridview.Rows[row_index].Cells[cell_index].Value.ToString();
                    }
                }
                text_box_export.Text = text_box_export.Text + "\r\n";
            }
            //writes new data from messagebox over stocklist.csv file
            System.IO.File.WriteAllText("c:\\Stockfile\\stocklist.csv", text_box_export.Text);
        }
        else
        {
            MessageBox.Show("Data not saved. Please enter only integers");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您的主要目标是

  

使我的DataGridView的“当前计数”列只接受整数

您可以处理dataGridView1.EditingControl.KeyPress事件并验证EventHandler method中的输入。
试试以下

private string patternToMatchIntegerValues = @"^\d*$";

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  my_datagridview.EditingControl.KeyPress -= EditingControl_KeyPress;
  my_datagridview.EditingControl.KeyPress += EditingControl_KeyPress;
}

private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
{
  if (!char.IsControl(e.KeyChar))
  {
    var editingControl = (Control)sender;

    if (!Regex.IsMatch(editingControl.Text + e.KeyChar, patternToMatchIntegerValues))
      // Stop the character from being entered into the control since it is non-numerical.
      e.Handled = true;
  }
}