here are the steps....
How to add the rows & columns in gridview problematically
here are the steps....
How to import data from excel file to grid view ?
we will be using OLEDB instead of SQL because we will be using excel file as database & not sqlserver.
In HTML code just take the gridview & on button named Import.
I will look something like this....
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="WebImportExport.aspx.cs"
Inherits="Forms_WebImportExport" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Import" runat="server"
OnClick="Import_Click" Text="Import" />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
</div>
</form>
</body>
</html>
In Code behind...
using System.Data.OleDb;
using System.IO;
public partial class Import_ExcelToGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Import_Click(object sender, EventArgs e)
{
OleDbconnection con = new OleDbconnection
(@"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\\path\\vaibhav.xls;
Extended Properties=\"Excel 8.0;\");
OleDbDataAdapter oleDA = new OleDbDataAdapter
("SELECT name , dept, salary from [sheet1$]", con);
DataSet ds = new DataSet();
oleDA.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
Here i will pass my excel file in string which have to be imported to the gridview.This will import he data from exel file to the gridview.
How to change the style of the row of gridview on mouse hover using css?
Steps:
1. Take one gridview from toolbox & bind the data to it.
2. run the page & viw the page source of the page. There you will find that one table is created for the gridview , so for each tr we can assign the style & which will take effect at runtime.
3. So in head of the page inside tag write the css.
You can write css something like this….this will change the background color of the row as well as font of the text in the row.
< style>
tr:hover{
color:Orange;
font:normal 16px Arial;
}
Exporting data from GridView to pdf file in asp.net or how to export data from gridview to pdf in c#?
In this demo tutorial I will show you how to export data from gridview to pdf file.
Steps are as follows:
1. Create new web page.
2. Drag & drop gridview from toolbox & bind your database (don’t use enable sorting).
<asp:GridView ID="GridView2" runat="server">
asp:GridView>
3. Drag & drop button.
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
4. You need to download one dll file named iTextSharp.dll.
5. Now add reference of this downloaded dll.
6. Now go to code behind & some of the following codes.
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
7. Now add following code in code behind.
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content0disposition",
"attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
GridView1.HeaderRow.Style.Add("width", "15%");
GridView1.HeaderRow.Style.Add("font-size", "10px");
GridView1.Style.Add("text-decoration", "none");
GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif");
GridView1.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
StreamReader reader = new StreamReader(new MemoryStream(Encoding.ASCII.Ge tBytes(sw.ToString())));
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(reader);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Now on button click , whole data from your gridview will be transferred in pdf file .