Click here to Skip to main content
16,019,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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();
Posted
Updated 8-Aug-11 3:41am
v3

1 solution

C#
DataRow newRow = dt.NewRow();
newRow[NAME] = "New row";
dt.Rows.Add(newRow);
 
Share this answer
 
v2
Comments
pradeep manne 8-Aug-11 9:39am    
Hi,srry i did not understood this one,plz
can u tell me in brief,i need to add details of another sheets selected from dropdown below the first row ....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900