Friday 17 February 2012

GridView with Example

GridView:
The GridView view mode displays a list of data items by binding data fields to columns and by displaying a column header to identify the field. The default GridView style implements buttons as column headers. By using buttons for column headers, you can implement important user interaction capabilities; for example, users can click the column header to sort GridView data according to the contents of a specific column.

The following illustration shows a GridView view of ListView content.

GridView view of ListView content

Styled ListView

GridView columns are represented by GridViewColumn objects, which can automatically size to their content. Optionally, you can explicitly set a GridViewColumn to a specific width. You can resize columns by dragging the gripper between column headers. You can also dynamically add, remove, replace, and reorder columns because this functionality is built into GridView. However, GridView cannot directly update the data that it displays.

The below code deals with inserting a data to a database and displaying the items to the grid aas well and this code mainly focuses on how to export the items on the grid to excel.

Here we deal with keywords like htmltextwriter and string writer, the string writer is mainly used for implementing htmltextwriter and the htmltextwriter is used for producing indented outputs and conversion purposes as well and the code is as follows

NOTE: set EnableEventValidation ="fase" at the top of your page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public partial class Default6 : System.Web.UI.Page
{
    string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connection);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Country values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        BindGrid();
    }
    private void BindGrid()
    {
        SqlConnection con = new SqlConnection(connection);
        con.Open();
        da = new SqlDataAdapter("Select * from Country", con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
   
    private void ExportGridView()
    {
        string attachment = "attachment; filename=Country.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

    }
   

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GridView1.Columns[2].Visible = false;
        GridView1.Columns[3].Visible = false;
        ExportGridView();
    }
    private void clear()
    {
        TextBox1.Text = string.Empty;
        TextBox2.Text = string.Empty;
        btnSave.Enabled = true;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        this.clear();
    }
}

2 comments:

  1. Replies
    1. If Possible can you please show us Video Tut of all the content which ur are sharing it would be very helpful

      Thanks

      Delete