Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2006, 05:14 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default dataGrid_ItemCommand

I am try to make possible , to delete row from a dataSet using dataGrid.

but my dataGrid_ItemCommand event is never fire,
my code is:

Code:
private void dgOrders_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
  if ( e.CommandName == "Delete")
  {
      int intOrderID = Convert.ToInt16
    (dgOrders.Items[e.Item.ItemIndex].Cells[0].Text);
      DataRow rowDelete = dsOrders1.Orders.NewRow();
      rowDelete = dsOrders1.Orders.FindByOrderID(intOrderID);
      rowDelete.Delete();
      dgOrders.DataBind();
  }

 
Old February 24th, 2006, 01:24 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

There could be two possibilities becuase of which the code is never executed. First, there is no handler for this event under web generated designer code region. Second thing could be that the datagrid is binded everytime the page is loaded. Please check if the datasource of the datagrid is set only if the the page is nt posted back

Lets c if that solves your problem..:)

 
Old February 24th, 2006, 05:57 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks but , I do not think these are the problems , I wrote the following code and it does not work :

this.dgOrders.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgOrders_ItemCommand);

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
    daOrders.Fill(dsOrders1);
    Session["dsOrders1"] = dsOrders1;
}
else
      dsOrders1 = (dsOrders)Session["dsOrders1"];
dgOrders.DataBind();
}

 
Old February 24th, 2006, 06:06 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi..got the actual problem after going through the code that you have written.
It actually is the problem of datagrid binding only that i was talking of in my last msg...

U have given dgOrders.dataBind() outside postback loop. Before going to the definition of Item Command EVent, Page Load is executed and ur datagrid is binded again and hence the item command event is not fired.
Modify your code a little bit and write it this way

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
    daOrders.Fill(dsOrders1);
    Session["dsOrders1"] = dsOrders1;
     dgOrders.DataBind();
}
else
      dsOrders1 = (dsOrders)Session["dsOrders1"];

}

Your DataBind command must be called just once only when the page is loaded first time.
This will for sure resolve the problem
:)


 
Old February 24th, 2006, 07:33 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank U for your help :)









Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Sub DataGrid_itemCommand when Page_Onload alyeng2000 ASP.NET 1.0 and 1.1 Basics 1 April 3rd, 2004 11:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.