将GridView数据放在DataTable中
我正在尝试将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
这将表明它有效。
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); }
将Grid复制到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
上述就是C#学习教程:将GridView数据放在DataTable中分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
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; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/999041.html