Hi all,
i have a proplem when updating my Datagrid :
I create a Datagrid with ( Delete ) and ( Update ) columns , Delete colume 
worke correctly but Update colume does not work - my code is
####################################
--- Html Code  ---WebForm3.aspx
 
 <%@ Import Namespace="System.Data" %>
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" 
Inherits="Pray.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <body>
  <form id="Form1" runat="server">
   <h3>DataGrid Editing Example</h3>
   <asp:datagrid id="ItemsGrid" runat="server" AutoGenerateColumns="False" 
CellPadding="3" BorderWidth="1px"
    BorderColor="Black">
    <HeaderStyle BackColor="#AAAADD"></HeaderStyle>
    <Columns>
     <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" 
HeaderText="Edit" CancelText="Cancel"
      EditText="Edit">
      <HeaderStyle Wrap="False"></HeaderStyle>
      <ItemStyle Wrap="False"></ItemStyle>
     </asp:EditCommandColumn>
     <asp:ButtonColumn Text="Delete" HeaderText="Delete" 
CommandName="Delete"></asp:ButtonColumn>
     <asp:BoundColumn DataField="orderid" HeaderText="Order 
ID"></asp:BoundColumn>
     <asp:BoundColumn DataField="unitprice" HeaderText="Unit 
Price"></asp:BoundColumn>
     <asp:BoundColumn DataField="quantity" HeaderText="Quantity">
      <HeaderStyle HorizontalAlign="Right"></HeaderStyle>
      <ItemStyle HorizontalAlign="Right"></ItemStyle>
     </asp:BoundColumn>
    </Columns>
   </asp:datagrid>
   <asp:Label id="Label1" runat="server">Label</asp:Label></form>
 </body>
</HTML>

###############################
--- WebForm3.aspx.cs --
 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Configuration.Assemblies;
namespace Pray
{
 /// <summary>
 /// Summary description for WebForm3.
 /// </summary>
 public class WebForm3 : System.Web.UI.Page
 {
  private string strConn=ConfigurationSettings.AppSettings.Get("MyConn2");
  private DataSet ds=new DataSet();
  private SqlDataAdapter da=new SqlDataAdapter();
  protected System.Web.UI.WebControls.DataGrid ItemsGrid;
  protected System.Web.UI.WebControls.Label Label1;
  DataView CartView;   
  private void Page_Load(object sender, System.EventArgs e)
  {
   GetSource();
  }
  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {  
   this.ItemsGrid.ItemCommand += new 
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ItemsGrid_ItemCommand);
   this.ItemsGrid.CancelCommand += new 
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ItemsGrid_CancelCommand);
   this.ItemsGrid.EditCommand += new 
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ItemsGrid_EditCommand);
   this.ItemsGrid.UpdateCommand += new 
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ItemsGrid_UpdateCommand);
   this.ItemsGrid.ItemDataBound += new 
System.Web.UI.WebControls.DataGridItemEventHandler(this.ItemsGrid_ItemDataBound);
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
  void GetSource()
  {
   SqlConnection Conn=new SqlConnection(strConn);
   SqlCommand cmdSelect=Conn.CreateCommand();
   cmdSelect.CommandType=CommandType.Text;
   cmdSelect.CommandText="SELECT OrderID, ProductID,UnitPrice, 
Quantity,UnitPrice * Quantity as cost FROM [Order Details] WHERE ProductID =11";
   da.SelectCommand=cmdSelect;
   da.Fill(ds,"Cart");
   ItemsGrid.DataSource=ds;
   ItemsGrid.DataMember="Cart";
   ItemsGrid.DataBind();
   return;
  }
  void DeleteItem(DataGridCommandEventArgs e)
  {
   // e.Item is the table row where the command is raised. For bound
   // columns, the value is stored in the Text property of a TableCell.
   TableCell itemCell = e.Item.Cells[2];
   string item = itemCell.Text;
   Label1.Text=item;
   // Remove the selected item from the data source.         
   if (ds.Tables["Cart"].Rows.Count > 0) 
   {  
    SqlConnection Conn=new SqlConnection(strConn);
    string strcmd="DELETE FROM [Order Details]where OrderID="+item;
    SqlCommand DeleCmd=new SqlCommand(strcmd,Conn);
    DeleCmd.CommandType=CommandType.Text;
    Conn.Open();
    DeleCmd.ExecuteNonQuery();
    Conn.Close();
    ds.Tables["Cart"].Clear();
   }
   GetSource();
  }
  private void ItemsGrid_EditCommand(object source, 
System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   // Set the EditItemIndex property to the index of the item clicked 
   // in the DataGrid control to enable editing for that item. Be sure
   // to rebind the DateGrid to the data source to refresh the control.
   ItemsGrid.EditItemIndex = e.Item.ItemIndex;
   ItemsGrid.DataSource=ds;
   ItemsGrid.DataMember="Cart";
   ItemsGrid.DataBind();
  }
  private void ItemsGrid_ItemCommand(object source, 
System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   switch(((LinkButton)e.CommandSource).CommandName)
   {
    case "Delete":
     DeleteItem(e);
     break;
     // Add other cases here, if there are multiple ButtonColumns in 
     // the DataGrid control.
    default:
     // Do nothing.
     break;
   }
  }
  private void ItemsGrid_UpdateCommand(object source, 
System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   // Retrieve the text boxes that contain the values to update.
   // For bound columns, the edited value is stored in a TextBox.
   // The TextBox is the 0th control in a cell's Controls collection.
   // Each cell in the Cells collection of a DataGrid item represents
   // a column in the DataGrid control.
   TextBox priceText = (TextBox)e.Item.Cells[3].Controls[0];
   TextBox qtyText = (TextBox)e.Item.Cells[4].Controls[0];
   TextBox oIdText = (TextBox)e.Item.Cells[2].Controls[0];
   // Retrieve the updated values.
   string item = e.Item.Cells[2].Text;
   string qty = qtyText.Text;
   string price = priceText.Text;
   // Set the EditItemIndex property to -1 to exit editing mode. 
   // Be sure to rebind the DateGrid to the data source to refresh
   // the control.
   ItemsGrid.EditItemIndex = -1;
   Label1.Text=oIdText.Text.Trim();
   ItemsGrid.DataSource=ds;
   ItemsGrid.DataMember="Cart";
   ItemsGrid.DataBind();
   // With a database, use an update command to update the data. 
   SqlConnection Conn=new SqlConnection(strConn);
   string strcmd1="update [Order Details] set 
UnitPrice="+Convert.ToDouble(price)+",Quantity="+ Convert.ToInt32(qty) +" where 
ProductID=11 and OrderID="+oIdText.Text.Trim();
   SqlCommand UpdCmd=new SqlCommand(strcmd1,Conn);
   UpdCmd.CommandType=CommandType.Text;
   Conn.Open();
   UpdCmd.ExecuteNonQuery();
   Conn.Close();
   //ds.Tables["Cart"].Clear();
   GetSource();
  }
  private void ItemsGrid_CancelCommand(object source, 
System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   // Set the EditItemIndex property to -1 to exit editing mode. 
   // Be sure to rebind the DateGrid to the data source to refresh
   // the control.
   ItemsGrid.EditItemIndex = -1;
   ItemsGrid.DataSource=ds;
   ItemsGrid.DataMember="Cart";
   ItemsGrid.DataBind();
  }
  private void ItemsGrid_ItemDataBound(object sender, 
System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if(e.Item.ItemType==ListItemType.Item || 
e.Item.ItemType==ListItemType.AlternatingItem)
    
e.Item.Cells[3].Text=String.Format("{0:c}",Convert.ToDouble(e.Item.Cells[3].Text));
  }

 }
}

################################
 
Please helpe me
 
regards,
Ahmed
 
 
 

                
---------------------------------
Do you Yahoo!?
 Yahoo! Mail - 250MB free storage. Do more. Manage less.

[Non-text portions of this message have been removed]



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to