2013-05-09 19 views
10

Próbuję zapisać GridView na DataTable.Umieszczanie danych GridView w DataTable

Mam kod, który teoretycznie powinien zrobić, ale wciąż otrzymuję ten błąd:

Column 0 doesn't exist

Tu jest mój kodu:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     DataTable dt = new DataTable(); 
     for (int j = 0; j < GridView1.Rows.Count; j++) 
     { 
      DataRow dr; 
      GridViewRow row = GridView1.Rows[j]; 
      dr = dt.NewRow(); 
      for (int i = 0; i < row.Cells.Count; i++) 
      { 
       dr[i] = row.Cells[i].Text; 
      } 

      dt.Rows.Add(dr); 
     } 
    } 

Każda pomoc, w jaki sposób rozwiązać ten problem?

+0

trzeba dodać taką samą liczbę kolumn jak Twój GridView mają .. pierwszy... –

Odpowiedz

9

można zrobić coś takiego:

DataTable dt = new DataTable(); 
for (int i = 0; i < GridView1.Columns.Count; i++) 
    { 
     dt.Columns.Add("column"+i.ToString()); 
    } 
foreach (GridViewRow row in GridView1.Rows) 
    { 
     DataRow dr = dt.NewRow(); 
     for(int j = 0;j<GridView1.Columns.Count;j++) 
      { 
       dr["column" + j.ToString()] = row.Cells[j].Text; 
      } 

      dt.Rows.Add(dr); 
    } 

A to pokaże, że to działa.

GridView6.DataSource = dt; 
GridView6.DataBind(); 
3
protected void btnExportExcel_Click(object sender, EventArgs e) 
{ 
    DataTable _datatable = new DataTable(); 
    for (int i = 0; i < grdReport.Columns.Count; i++) 
    { 
     _datatable.Columns.Add(grdReport.Columns[i].ToString()); 
    } 
    foreach (GridViewRow row in grdReport.Rows) 
    { 
     DataRow dr = _datatable.NewRow(); 
     for (int j = 0; j < grdReport.Columns.Count; j++) 
     { 
      if (!row.Cells[j].Text.Equals("&nbsp;")) 
       dr[grdReport.Columns[j].ToString()] = row.Cells[j].Text; 
     } 

     _datatable.Rows.Add(dr); 
    } 
    ExportDataTableToExcel(_datatable); 
} 
0

użytkownik to pełne rozwiązanie do konwersji GridView do elementu datatable

public DataTable gridviewToDataTable(GridView gv) 
     { 

      DataTable dtCalculate = new DataTable("TableCalculator"); 

      // Create Column 1: Date 
      DataColumn dateColumn = new DataColumn(); 
      dateColumn.DataType = Type.GetType("System.DateTime"); 
      dateColumn.ColumnName = "date"; 

      // Create Column 3: TotalSales 
      DataColumn loanBalanceColumn = new DataColumn(); 
      loanBalanceColumn.DataType = Type.GetType("System.Double"); 
      loanBalanceColumn.ColumnName = "loanbalance"; 


      DataColumn offsetBalanceColumn = new DataColumn(); 
      offsetBalanceColumn.DataType = Type.GetType("System.Double"); 
      offsetBalanceColumn.ColumnName = "offsetbalance"; 


      DataColumn netloanColumn = new DataColumn(); 
      netloanColumn.DataType = Type.GetType("System.Double"); 
      netloanColumn.ColumnName = "netloan"; 


      DataColumn interestratecolumn = new DataColumn(); 
      interestratecolumn.DataType = Type.GetType("System.Double"); 
      interestratecolumn.ColumnName = "interestrate"; 

      DataColumn interestrateperdaycolumn = new DataColumn(); 
      interestrateperdaycolumn.DataType = Type.GetType("System.Double"); 
      interestrateperdaycolumn.ColumnName = "interestrateperday"; 

      // Add the columns to the ProductSalesData DataTable 
      dtCalculate.Columns.Add(dateColumn); 
      dtCalculate.Columns.Add(loanBalanceColumn); 
      dtCalculate.Columns.Add(offsetBalanceColumn); 
      dtCalculate.Columns.Add(netloanColumn); 
      dtCalculate.Columns.Add(interestratecolumn); 
      dtCalculate.Columns.Add(interestrateperdaycolumn); 

      foreach (GridViewRow row in gv.Rows) 
      { 
       DataRow dr; 
       dr = dtCalculate.NewRow(); 

       dr["date"] = DateTime.Parse(row.Cells[0].Text); 
       dr["loanbalance"] = double.Parse(row.Cells[1].Text); 
       dr["offsetbalance"] = double.Parse(row.Cells[2].Text); 
       dr["netloan"] = double.Parse(row.Cells[3].Text); 
       dr["interestrate"] = double.Parse(row.Cells[4].Text); 
       dr["interestrateperday"] = double.Parse(row.Cells[5].Text); 


       dtCalculate.Rows.Add(dr); 
      } 



      return dtCalculate; 
     } 
3

Kopiowanie Siatka do elementu datatable

 if (GridView.Rows.Count != 0) 
     { 
      //Forloop for header 
      for (int i = 0; i < GridView.HeaderRow.Cells.Count; i++) 
      { 
       dt.Columns.Add(GridView.HeaderRow.Cells[i].Text); 
      } 
      //foreach for datarow 
      foreach (GridViewRow row in GridView.Rows) 
      { 
       DataRow dr = dt.NewRow(); 
       for (int j = 0; j < row.Cells.Count; j++) 
       { 
        dr[GridView.HeaderRow.Cells[j].Text] = row.Cells[j].Text; 
       } 
       dt.Rows.Add(dr); 
      } 
      //Loop for footer 
      if (GridView.FooterRow.Cells.Count != 0) 
      { 
       DataRow dr = dt.NewRow(); 
       for (int i = 0; i < GridView.FooterRow.Cells.Count; i++) 
       { 
        //You have to re-do the work if you did anything in databound for footer. 
       } 
       dt.Rows.Add(dr); 
      } 
      dt.TableName = "tb"; 
     }