Dynamic datagrid EditCommand problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.DataGridCommandEventHandler(this.Edit_Data);
myGrid.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.Cancel_Data);
myGrid.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.Save_Data);

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

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

Anyone knows what is wrong here?
 
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
 
Back
Top