ASP.NET
  Home arrow ASP.NET arrow Page 6 - How to Play with DataGrid Control
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

How to Play with DataGrid Control
By: Mayank Gupta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 53
    2004-06-16

    Table of Contents:
  • How to Play with DataGrid Control
  • How the Work is Done
  • The Page_Load Event Handler
  • Editing
  • Handling Item Edit Events
  • Handling Multiple Row Selection for Deletion by Checkbox Control
  • Finishing Up

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Play with DataGrid Control - Handling Multiple Row Selection for Deletion by Checkbox Control


    (Page 6 of 7 )

    How to add CheckBox with DataGrid using TemplateColoumn

    <asp:TemplateColumn>
     <HeaderTemplate>
      <input type="CheckBox" name="SelectAllCheckBox" onclick="SelectAll(this)">
     </HeaderTemplate>
     <ItemTemplate>
      <asp:CheckBox id="SelectCheckBox" Runat="Server" />
     </ItemTemplate>
    </asp:TemplateColumn
    >

    <HeaderTemplate> will show a checkbox named as SelectAllCheckBox on DataGrid Header. And a JavaScript code will be execute OnClick event, i.e., SelectAll(this).

    function SelectAll(CheckBoxControl)
    {
     if (CheckBoxControl.checked == true)
     {
      var i;
      for (i=0; i < document.forms[0].elements.length; i++)
      {
       if ((document.forms[0].elements[i].type == 'checkbox') &&  (document.forms[0].elements[i].name.indexOf('dgNonApproveList') > -1))
       {
        document.forms[0].elements[i].checked = true;
       }
      }
     }
     else
     {
      var i;
      for (i=0; i < document.forms[0].elements.length; i++)
      {
       if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].name.indexOf('dgNonApproveList') > -1))
       {
        document.forms[0].elements[i].checked = false;
       }
      }
     }
    }

    That function selects either all CheckBox Control or deselects all CheckBox Control.

    Handling the btnApprove_Click Events (for delete particular row as well as multiple row deletion)

    string GetSelectedUsers()
    {
     int i;
     for (i=0; i < dgNonApproveList.Items.Count; i++)
     {
      if ( ( (CheckBox)dgNonApproveList.Items[i].FindControl("SelectCheckBox")).Checked == true)
      {
       if (SelUsersId != "")
       {
        SelUsersId += ",";
       }
       SelUsersId += dgNonApproveList.DataKeys[i];
      }
     }
     return SelUsersId;
    }
    void btnApprove_Click(object sender, System.EventArgs e)
    {
     string SelUsersId = GetSelectedUsers();
     if (SelUsersId!=String.Empty)
     {
      string strConn=System.Configuration.ConfigurationSettings.AppSettings["strConn"];
      SqlConnection sqlConn = new SqlConnection(strConn);
      sqlConn.Open();
      SqlCommand objCommand = new SqlCommand();
      objCommand.Connection=sqlConn;
      SqlTransaction myTrans = sqlConn.BeginTransaction();
      objCommand.Transaction=myTrans;
      try
      {
       objCommand.CommandText="Delete From DocTemplate Where DocTId in ("+ SelUsersId +")";
       objCommand.ExecuteNonQuery();
       objCommand.CommandText="Delete From UserDocDefault Where DocTId in ("+ SelUsersId +")";
       objCommand.ExecuteNonQuery();
       myTrans.Commit();
      }
      catch (SqlException SqlEx)
      {
       MessageLabel.Text = SqlEx.Message;
       myTrans.Rollback();
      }
      catch (Exception Ex)
      {
       MessageLabel.Text = Ex.Message;
      }
      finally
      {
       sqlConn.Close();
      }
      BindGrid();
     }
     else
      MessageLabel.Text = "Pls Select at least one User Id.";
    }

    The above code you have seen will work for delete multiple rows from the DataGrid Control.

    More ASP.NET Articles
    More By Mayank Gupta


     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek