Showing posts with label Gridview. Show all posts
Showing posts with label Gridview. Show all posts

Sunday, December 25, 2011

Bulk Copy Operation


Bulk Copy Operation

This code shows you how to do bulk copy operation using C#.
string connectionString = "";
 using (SqlConnection sc = new SqlConnection(connectionString))
 {
     sc.Open();
 
     SqlCommand commandSourceData = new SqlCommand("SELECT * FROM StoresBackup;", sc);
     SqlDataReader reader =  commandSourceData.ExecuteReader();
     using (SqlConnection dc = new  SqlConnection(connectionString))
     {
         dc.Open();
         using (SqlBulkCopy bulkCopy = new SqlBulkCopy(dc))
         {
              bulkCopy.DestinationTableName = "Stores";
              bulkCopy.WriteToServer(reader);
         }
     }
 }

Thursday, December 8, 2011

GridView with CheckBox for Deleteing the checked Items..


Take This Items In webForms

- One gridView

- One button checkAll/UncheckAll

- One Button for Delete all the Checked CheckBox..

Lets see one by one how can we put checkbox inside GridView

Use This Source File In Html 

[asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"]                       
 
[Columns]
 
   [asp:BoundField DataField="roll_no" HeaderText="Roll No" /]
 
   [asp:BoundField DataField="s_name" HeaderText="Student Name" /]
 
   [asp:BoundField DataField="class" HeaderText="Class" /]
 
     [asp:TemplateField]
 
   [ItemTemplate]
 
       [asp:CheckBox ID="ch1" Text='<%# Eval("roll_no") %>' runat="server" /]
 
     [/ItemTemplate]
 
      [/asp:TemplateField]
 
[/Columns]
 
  [/asp:GridView]


In your .cs file in the class level write this line of code

First Use This Code in Page load
ArrayList alist = new  ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
   {
      //Populate data and Bind in Gridview
 
      }
 
   }


Use This Code In Check All Button Click Event

Wednesday, March 16, 2011

Use Check Box In Gridview In Asp.net C#

In Aspx .Page Take Grid view Like Below

<head runat="server">
    <title>Grid CheckBox</title>
  
    <script type="text/javascript">
        function SelectAll(id)
        {
            //get reference of GridView control
            var grid = document.getElementById("<%= GridView1.ClientID %>");
            //variable to contain the cell of the grid
            var cell;
           
            if (grid.rows.length > 0)
            {
                //loop starts from 1. rows[0] points to the header.
                for (i=1; i<grid.rows.length; i++)
                {
                    //get the reference of first column
                    cell = grid.rows[i].cells[0];
                   
                    //loop according to the number of childNodes in the cell
                    for (j=0; j<cell.childNodes.length; j++)
                    {          
                        //if childNode type is CheckBox                
                        if (cell.childNodes[j].type =="checkbox")
                        {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                            cell.childNodes[j].checked = document.getElementById(id).checked;
                        }
                    }
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField>
                    <AlternatingItemTemplate>
                        <asp:CheckBox ID="cbSelectAll" runat="server" />
                    </AlternatingItemTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="cbSelectAll" runat="server" Text="Select All" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbSelectAll" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="ApplicationName" HeaderText="Application Name" />
                <asp:BoundField DataField="ServerName" HeaderText="Server Name" />
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
In Aspx.Cs Page Use this Code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Binding GridView with the datasource
            this.GridView1.DataSource = FillDataTable();
            this.GridView1.DataBind();
        }
    }

    /// <summary>
    /// Method that will add few rows in a DataTable and returns a DataTable
    /// </summary>
    /// <returns>DataTable</returns>
    protected DataTable FillDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ApplicationID");
        dt.Columns.Add("ApplicationName");
        dt.Columns.Add("ServerName");

        DataRow dr = dt.NewRow();
        dr[0] = "1";
        dr[1] = "Application 1";
        dr[2] = "Server 1";
        dt.Rows.Add(dr);

        DataRow dr1 = dt.NewRow();
        dr1[0] = "2";
        dr1[1] = "Application 2";
        dr1[2] = "Server 2";
        dt.Rows.Add(dr1);

        DataRow dr2 = dt.NewRow();
        dr2[0] = "3";
        dr2[1] = "Application 3";
        dr2[2] = "Server 3";
        dt.Rows.Add(dr2);

        DataRow dr3  = dt.NewRow();
        dr3[0] = "4";
        dr3[1] = "Application 4";
        dr3[2] = "Server 4";
        dt.Rows.Add(dr3);

        DataRow dr4 = dt.NewRow();
        dr4[0] = "5";
        dr4[1] = "Application 5";
        dr4[2] = "Server 5";
        dt.Rows.Add(dr4);

        DataRow dr5 = dt.NewRow();
        dr5[0] = "6";
        dr5[1] = "Application 6";
        dr5[2] = "Server 6";
        dt.Rows.Add(dr5);

        return dt;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Find the checkbox control in header and add an attribute
            ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");
        }
    }
}

Tuesday, September 14, 2010

Export to Excel in ASP.Net 2.0 – Gridview to Excel, DataTable to Excel

Exporting contents to excel or preparing a excel report of the data displayed on a webpage is one of the most common tasks in real world web applications. Most frequently, we will export the contents of GridView control to excel. In this article, i will implement some of the common ways of exporting a table of data displayed on a web page to a excel file.

Export to Excel
Ø       Rendering the Gridview Control to Excel
Ø       Rendering the underlying DataTable to Excel

Rendering the Gridview Control to Excel
This is one of the most commonly done approach where we set MIME type and use Gridview’s RenderControl() method, similar to we do for a Datagrid control.
        string attachment = "attachment; filename=Employee.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter stw = new StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        gvEmployee.RenderControl(htextw);
        Response.Write(stw.ToString());
        Response.End();

When we execute the above code, it will give the following error.


view more : http://www.codedigest.com/Articles/ASPNET/130_Export_to_Excel_in_ASPNet_20_%E2%80%93Gridview_to_Excel_DataTable_to_Excel.aspx

Friday, September 10, 2010

How to Bind Data to a DataGrid Control Using ASP.Net and VB.Net,How To Bind Data In Datagrid From Database?

DataGrid control. 

Html Source Code : 
<asp:Label ID="lblOrderDetails" Runat="server" /><br><br>



<asp:DataGrid ID="dgDisplayData" Runat="server" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="True" AllowPaging="True" AllowSorting="True">

 <FooterStyle ForeColor="Black" BackColor="#CCCCCC" />

 <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C" />

 <AlternatingItemStyle BackColor="#DCDCDC" />

 <ItemStyle ForeColor="Black" BackColor="#EEEEEE" />

 <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084" />

 <PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#999999" />

asp:DataGrid><br>



<hr align="left" width="375" size="1">


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    DBConn.Open()

    If Not IsPostBack Then

      DBAdap = New SqlDataAdapter("Select OrderID from Orders", DBConn)

      DBAdap.Fill(DS, "Orders")

      ddlSelectOrder.DataSource = DS.Tables("Orders").DefaultView

      ddlSelectOrder.DataBind()

      ddlSelectOrder.Items.Add(New ListItem("", ""))

      ddlSelectOrder.SelectedValue = ""

    End If

End Sub


Now bind DataGrid control when the value is selected from the DropDownList control. The event used for this task is ddlSelectOrder_SelectedIndexChanged:



Private Sub ddlSelectOrder_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSelectOrder.SelectedIndexChanged



    Dim orderID As Integer

    If ddlSelectOrder.SelectedValue = String.Empty Then

      lblOrderDetails.Text = ""

      dgDisplayData.DataSource = Nothing

      Me.DataBind()

      Exit Sub

    End If

    orderID = ddlSelectOrder.SelectedValue

    lblOrderDetails.Text = "Order's Detail is as follows:"

    DBAdap = New SqlDataAdapter("SELECT * From OrderDetails WHERE OrderID = " & orderID, DBConn)

    DBAdap.Fill(DS, "OrderDetails")

    dgDisplayData.DataSource = DS.Tables("OrderDetails").DefaultView

    dgDisplayData.DataBind()



End Sub

Saturday, April 24, 2010

GridView with CheckBox for Deleteing the checked Items.

 Use This Source File In Html

[asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"]                     

[Columns]

   [asp:BoundField DataField="roll_no" HeaderText="Roll No" /]

   [asp:BoundField DataField="s_name" HeaderText="Student Name" /]

   [asp:BoundField DataField="class" HeaderText="Class" /]

     [asp:TemplateField]

   [ItemTemplate]

       [asp:CheckBox ID="ch1" Text='<%# Eval("roll_no") %>' runat="server" /]

     [/ItemTemplate]

      [/asp:TemplateField]

[/Columns]

  [/asp:GridView]


In your .cs file in the class level write this line of code

First Use This Code in Page load
ArrayList alist = new  ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
   {
      //Populate data and Bind in Gridview

      }

   }

Sunday, April 11, 2010

GridView with CheckBox for Deleteing the checked Items..

[asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"]                      
[Columns]
   [asp:BoundField DataField="roll_no" HeaderText="Roll No" /]
   [asp:BoundField DataField="s_name" HeaderText="Student Name" /]
   [asp:BoundField DataField="class" HeaderText="Class" /]
     [asp:TemplateField]
  [ItemTemplate]
       [asp:CheckBox ID="ch1" Text='<%# Eval("roll_no") %>' runat="server" /]
   [/ItemTemplate]
      [/asp:TemplateField]
[/Columns]
  [/asp:GridView]