GridView
をDataTable
に保存しようとしています。
私は理論的にそれを行う必要があるコードを持っていますが、このエラーが発生し続けます:
列0は存在しません
ここに私のコードがあります:
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);
}
}
これを修正する方法についてのヘルプはありますか?
次のようなことができます:
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);
}
そして、それはそれが機能することを示します。
GridView6.DataSource = dt;
GridView6.DataBind();
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(" "))
dr[grdReport.Columns[j].ToString()] = row.Cells[j].Text;
}
_datatable.Rows.Add(dr);
}
ExportDataTableToExcel(_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";
}
gridviewを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;
}