Esempio n. 1
0
    /// <summary>
    /// 把DataGridView的数据复制到DataTable
    /// </summary>
    /// <param name="dgv">dgv控件作为参数</param>
    /// <returns>返回临时内存表</returns>
    public static DataTable GetDgvToTable(DataGridView dgv)
    {
        DataTable dt = new DataTable();

        for (int count = 0; count < dgv.Columns.Count; count++)
        {
            DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
            dt.Columns.Add(dc);
        }

        for (int count = 0; count < dgv.Rows.Count; count++)
        {
            if (dgv.Rows[count].IsNewRow == false)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i < dgv.Columns.Count; i++)
                {
                    dr[i] = ConvertHelper.CastString(dgv.Rows[count].Cells[i].Value);
                }
                dt.Rows.Add(dr);
            }
        }
        return(dt);
    }