Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts

How to add the rows & columns in gridview problematically

n this tutorial i will show you that how can wee add rows & column in the gridview using coding or problematically
here are the steps....


1) Create a DataTable object to which than we will bind the GridView

DataTable dt = new DataTable();
2) IF you need two columns than create two DataColumn objects

DataColumn dc1 = new DataColumn("first", typeof(string));

DataColumn dc2 = new DataColumn("second", typeof(string));
DataColumn dc3 = new DataColumn("third", typeof(string));

Add it to the table

dt.Columns.Add(dc1);

dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
3)Run the loop for as many rows you want to add. eg: 3 rows to be added.

for(int i=0;i<3;i++)// 3 = number of rows needed.

{
DataRow row1 = dt.NewRow();
int c = 0;
while (c < 3)
{
row1[c] = "some_data";
c++;
}
dt.Rows.Add(row1 );
}

Now iterate through each datacolumn and create a BoundField foreach column

foreach (DataColumn col in dt.Columns)

{
BoundField bField = new BoundField();
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}

GridView1.DataSource = dt;
//Bind the datatable with the GridView.
GridView1.DataBind();

How to import data from excel file to grid view ?

Here i will show that how can we import data from the excel file to gridview....
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);
GridView
1.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?

Changing style of the row of gridview on mouse hover.
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#?

<!--[if gte mso 9]> Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

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 .