How can I export a GridView.DataSource to a datatable or dataset?

后端 未结 7 1145
心在旅途
心在旅途 2020-11-28 08:45

How can I export GridView.DataSource to datatable or dataset?

7条回答
  •  失恋的感觉
    2020-11-28 08:55

    Ambu,

    I was having the same issue as you, and this is the code I used to figure it out. Although, I don't use the footer row section for my purposes, I did include it in this code.

        DataTable dt = new DataTable();
    
        // add the columns to the datatable            
        if (GridView1.HeaderRow != null)
        {
    
            for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            {
                dt.Columns.Add(GridView1.HeaderRow.Cells[i].Text);
            }
        }
    
        //  add each of the data rows to the table
        foreach (GridViewRow row in GridView1.Rows)
        {
            DataRow dr;
            dr = dt.NewRow();
    
            for (int i = 0; i < row.Cells.Count; i++)
            {
                dr[i] = row.Cells[i].Text.Replace(" ","");
            }
            dt.Rows.Add(dr);
        }
    
        //  add the footer row to the table
        if (GridView1.FooterRow != null)
        {
            DataRow dr;
            dr = dt.NewRow();
    
            for (int i = 0; i < GridView1.FooterRow.Cells.Count; i++)
            {
                dr[i] = GridView1.FooterRow.Cells[i].Text.Replace(" ","");
            }
            dt.Rows.Add(dr);
        }
    

提交回复
热议问题