473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamic datagrid EditCommand problem

Hi,

I created a Dynamic Datagrid and i added an EditCommandColumn to it.
Works fine, but my Editcommand eventhandler seems to have a problem with
PostBack
This is my code
private DataGrid GridDataLangs(DataTable vDataTable){
DataGrid myGrid = new DataGrid();
myGrid.AutoGenerateColumns = false;

//Add Columns
BoundColumn colDate = new BoundColumn();
colDate.HeaderText = "Date";
colDate.DataField = ("DateTimeFK");

EditCommandColumn colEditCommand = new EditCommandColumn();
colEditCommand.ButtonType = ButtonColumnType.LinkButton;
colEditCommand.UpdateText = "Update";
colEditCommand.EditText = "Edit";
colEditCommand.CancelText = "cancel";

myGrid.Columns.Add(colDate);
myGrid.Columns.Add(colEditCommand);

myGrid.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.Edit_Data);
myGrid.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.Cancel_Data);
myGrid.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.Save_Data);

myGrid.DataSource = vDataTable;
myGrid.DataBind();
return myGrid ;
}

private void Edit_Data(object
sender,System.Web.UI.WebControls.DataGridCommandEv entArgs e){
if(Page.IsPostBack){
((DataGrid)sender).EditItemIndex = e.Item.ItemIndex;
this.lblInfo.Text = e.Item.ItemIndex.ToString();
}
}

Anyone knows what is wrong here?
Nov 19 '05 #1
1 1814
Hi,

DataGrid needs to be added to the Controls collection before binding it (or
generally setting any properties), therefore binding and after returning it
from method and adding to Controls collection won't work.

Second because it is a dynamical control, it needs to be recreated and added
to the controls collection in Page_Load at the latest, so that postback
events will be raised.

See this article
Dynamic Web Controls, Postbacks, and View State
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"benoit" <be****@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
Hi,

I created a Dynamic Datagrid and i added an EditCommandColumn to it.
Works fine, but my Editcommand eventhandler seems to have a problem with
PostBack
This is my code
private DataGrid GridDataLangs(DataTable vDataTable){
DataGrid myGrid = new DataGrid();
myGrid.AutoGenerateColumns = false;

//Add Columns
BoundColumn colDate = new BoundColumn();
colDate.HeaderText = "Date";
colDate.DataField = ("DateTimeFK");

EditCommandColumn colEditCommand = new EditCommandColumn();
colEditCommand.ButtonType = ButtonColumnType.LinkButton;
colEditCommand.UpdateText = "Update";
colEditCommand.EditText = "Edit";
colEditCommand.CancelText = "cancel";

myGrid.Columns.Add(colDate);
myGrid.Columns.Add(colEditCommand);

myGrid.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.Edit_Data);
myGrid.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.Cancel_Data);
myGrid.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.Save_Data);

myGrid.DataSource = vDataTable;
myGrid.DataBind();
return myGrid ;
}

private void Edit_Data(object
sender,System.Web.UI.WebControls.DataGridCommandEv entArgs e){
if(Page.IsPostBack){
((DataGrid)sender).EditItemIndex = e.Item.ItemIndex;
this.lblInfo.Text = e.Item.ItemIndex.ToString();
}
}

Anyone knows what is wrong here?

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are...
4
by: Richard Roche | last post by:
Is it possible to use a drop combo instead of a text box when using the EditCommand in the Datagrid? Many table columns are bound to 'lookup' tables, user's don't care about the foreign keys,...
0
by: Chad Folden | last post by:
I can't figure out how to use the EditCommand for the nested datagrid .. HELP PLEASE ;-) It works perfectly for the parent datagrid, but will not display, even though the EditCommand event is...
0
by: Dan C Douglas | last post by:
I have created a hierarchical datagrid that consists of 1 datagrid nested insde of another datagrid. More technically it is actually 1 User control that contains a datagrid. I add this user control...
7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
1
by: MrMike | last post by:
I have a sub named Bind("") which binds my a datagrid on my webform. In order to enter edit mode and edit records on this datagrid, I must make a call to Bind("") either before or after I place...
5
by: Tina | last post by:
the Edit, Update, Cancel, and Delete buttons in my datagrid are causing validation elsewhere on the page. I want to specify that these buttons should not cause validation but they have no design...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.