ASP.NET
  Home arrow ASP.NET arrow Page 5 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 Item Edit Events


    (Page 5 of 7 )

    The EditCommand event is raised when the user clicks the Edit link in any row within the grid. For this event, we specified our dgNonApproveList_EditCommand event handler routine. Then we set the EditItemIndex property of the DataGrid control to the index of the row that contained the edit link the user clicked:

    void dgNonApproveList_EditCommand(Object s, DataGridCommandEventArgs e)
    {
     dgNonApproveList.EditItemIndex=e.Item.ItemIndex;
     BindGrid();
    }

    Handling the Update and Cancel Events

    Now that we have the grid in “edit mode”, we just need to handle the update and cancel events. We specified that a click on the cancel link should execute our event handler named dgNonApproveList_CancleCommand. In this event handler, all we need to do is switch the grid back out of “edit mode”  by existing the EditItemIndex property back to –1.

    void dgNonApproveList_CancelCommand(Object s, DataGridCommandEventArgs e)
    {
     dgNonApproveList.EditItemIndex=-1;
     BindGrid();
    }

    However, if the user clicks the update link, our dgNonApproveList_UpdateCommand will be called. Here we have to create a suitable SQL UPDATE statement in our example. For this we need to get the edited values from the DataGrid row that the user working on. Because we are updating dates, we are using Calendar. So it should not to go for validation test. In the case of textbox, we need to have validation. After declaring two variables to hold references to the calendar that contain the edited values, we first access the calDateFrom calendar using the FindControl method of the item that is contained in DataGridCommandEventArgs object passes to our event handler as a parameter. Notice that we have to convert the return value to the correct type like Calendar. It goes similar for second control that is calToDate.

    Now that we have the reference of two Calendar control, we can create an SQL UPDATE statement and call our Command Object and its ExecuteNonQuery.

    void dgNonApproveList_UpdateCommand(Object s, DataGridCommandEventArgs e)
    {
     string DocTId = dgNonApproveList.DataKeys[e.Item.ItemIndex].ToString();
     string strConn=System.Configuration.ConfigurationSettings.AppSettings["strConn"];
     Calendar calFrom;
     Calendar calTo;
     calFrom = (Calendar)e.Item.FindControl("calDateFrom");
     calTo = (Calendar)e.Item.FindControl("calDateTo");
     DateTime dtFrom = calFrom.SelectedDate.Date;
     DateTime dtTo = calTo.SelectedDate.Date;
     if (dtFrom<dtTo)
     {
      SqlConnection sqlConn = new SqlConnection(strConn);
      sqlConn.Open();
      SqlCommand objCommand = new SqlCommand();
      objCommand.Connection=sqlConn;
      SqlTransaction myTrans = sqlConn.BeginTransaction();
      objCommand.Transaction=myTrans;
      try
      {
       objCommand.CommandText="Update UserDocDefault Set DateTo='"+ dtFrom +"', DateTill='"+ dtTo +"' Where DocTId = "+             DocTId +"";
       objCommand.ExecuteNonQuery();
       myTrans.Commit();
      }
      catch (SqlException SqlEx)
      {
       MessageLabel.Text = SqlEx.Message;
       myTrans.Rollback();
      }
      catch (Exception Ex)
      {
       MessageLabel.Text = Ex.Message;
      }
      finally
      {
       sqlConn.Close();
      }
      dgNonApproveList.EditItemIndex=-1;
      BindGrid();
     }
     else
      MessageLabel.Text = "Date From must be lower than Date Till";
    }

    We finish off by the grid back out of “edit mode” by setting the EditItemIndex property of the DataGrid control back to -1, and rebind the control to display the result.

    More ASP.NET Articles
    More By Mayank Gupta


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT