0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DataGridViewのデータをDataTableにコピーする処理

Posted at
CopyDGVtoDT.cs

private DataTable CopyDGVtoDT(DataTable pdt, DataGridView dgv)
{
    DataTable dt = new DataTable();

    //列名コピー
    for (int i = 0; i < dgv.ColumnCount; i++)
    {
        pdt.Columns.Add(dgv.Columns[i].HeaderText);
    }

    //データコピー
    for (int dgvrow = 0; dgvrow < dgv.RowCount; dgvrow++)
    {
        //Datatableの行追加宣言
        DataRow dr = pdt.NewRow();

        for (int col = 0; col < dgv.ColumnCount; col++)
        {
            dr[col] = dgv.Rows[dgvrow].Cells[col].Value;
        }
        
        pdt.Rows.Add(dr);        //DataRowCollectionに登録
    }
}

DataRow dr = pdt.NewRow(); について、詳細説明ページ:
[Link]: https://resanaplaza.com/datatable%E3%81%AE%E5%9F%BA%E7%A4%8E%E3%81%A8%E4%BD%BF%E3%81%84%E6%96%B9/

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?