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  
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 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

    - 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek