本文介绍了向gridview添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在一个项目中,该项目将上传excelfile
和所有的Excel工作表名称都绑定到一个下拉列表,我想选择一个excelshhet并添加到gridview,这将向网格添加诸如excelname,行数和数字列以及sheetname等的详细信息.一旦我从下拉列表中选择了另一个工作表
我需要在第一行下方添加工作表的相同详细信息...
我为一张工作表编写了代码,如何在第一行的下面将下一个工作表详细信息添加到gridview
Hi ,
am working on a project which will upload excelfile
and all the excel sheets names are bind to a dropdown,i want to select a excelshhet and add to gridview which will add details like excelname,number of rows and number columns and sheetname etc.. to grid and once i select another sheet from dropdown
i need to add same details of the sheet below the first row .....
i written code for one sheet ,how to add next sheet details to gridview ,below the first row
string ds = Session["nam"].ToString();
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + ds + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(conStr);
con.Open();
System.Data.DataTable dt = null;
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
Response.Write("no shhet found"); ;
}
DataTable table = new DataTable();
//DataRow dr = null;
DataRow row=null;
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "id";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "item";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "item2";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "item3";
table.Columns.Add(column);
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + DropDownList1.SelectedItem.ToString() + "]", con);
DataSet objDataSet = new DataSet();
DataTable dt1 = new DataTable();
adapter.Fill(objDataSet);
dt1 = objDataSet.Tables[0];
adapter.Fill(dt1);
row = table.NewRow();
int number_of_columns = dt1.Columns.Count;
// Response.Write(number_of_columns);
int number_of_rows = objDataSet.Tables[0].Rows.Count / 2;
row["item"] = DropDownList1.SelectedItem.ToString();
row["item2"] = number_of_rows;
row["item3"] = number_of_columns;
row["id"] = LabelUpload.Text;
// table.NewRow();
table.Rows.Add(row);
//table.Rows.Add(table.NewRow());
GridView1.DataSource =table;
GridView1.DataBind();
推荐答案
DataRow newRow = dt.NewRow();
newRow[NAME] = "New row";
dt.Rows.Add(newRow);
这篇关于向gridview添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!