hi ,
iam adding an itemtemplate in the grid the contain the edit and delete buttons with
TemplateDataField field1 = new TemplateDataField(); field1.Key = "TemplateColumn1"; field1.Header.Text = "Edition "; this.WebDataGrid1.Columns.Add(field1); //get reference to the template column TemplateDataField templateColumn1 = (TemplateDataField)this.WebDataGrid1.Columns["TemplateColumn1"]; templateColumn1.ItemTemplate = new CustomItemTemplate();
and
private class CustomItemTemplate : ITemplate { #region ITemplate Members public void InstantiateIn(Control container) { Button edit = new Button(); edit.CssClass = "Button"; edit.Text = "Modifier"; edit.OnClientClick = "return editRow()"; Button delete = new Button(); delete.CssClass = "Button"; delete.Text = "Supprimer"; delete.OnClientClick = "return deleteRow()"; container.Controls.Add(edit); container.Controls.Add(delete); } #endregion }
but i get in this.WebDataGrid1.Columns.Add(field1); line when i clique any button
to put this another way how to do this in codebehind
<Columns> <ig:TemplateDataField Key="TemplateColumn0"> <Header Text="Edition HTML"></Header> <ItemTemplate> <asp:Button ID="Buttonedit" runat="server" Text="Edit HTML" onclientclick="eturn editRow();" /> <asp:Button ID="Buttondelete" runat="server" Text="Delete HTML" onclientclick="return deleteRow();" /> </ItemTemplate> </ig:TemplateDataField> </Columns>
Hi,
If you add the TemplateField in the code behind have to add it only when the page is not inPostback.
The other way is to check if column with this key exists and if not then to add it.
hi,
iam doing it by this in the view :
<Columns> <ig:TemplateDataField Key="TemplateColumn0"> <Header Text="Edit></Header> <ItemTemplate>
<asp:Button ID="Buttonedit" runat="server" Text="Edit" onclientclick="eturn editRow();" />
<asp:Button ID="Buttondelete" runat="server" Text="Delete onclientclick="return deleteRow();" />
</ItemTemplate> </ig:TemplateDataField> </Columns>
but i need to do it in the code behind , how?
In the Page_load you have to check if the page is postback and if it is to add the template column.
I am sending you a small sample with this:
where is the sample?
Sorry, I will send it again:
WDGCreateTemplate.zip
it worked , but when i update anyrow the data in the last slected row doesn't change
how can i force the grid to bind the data again before thepost back ?
Can you send me a small sample how your grid is databaund? You can modify this one which I send you.
Thanks
private DataTable Table() { string SQL; SqlDataAdapter da; DataTable dt; SQL = "SELECT [ID_DC_L]FROM[dbo].[DEVIS_CLIENT_LIGNE] where DC_ID = 4"; CN = new SqlConnection("Server=VS032=; Database=; User ID=; Password=; "); CN.Open(); CMD.CommandType = System.Data.CommandType.Text; CMD.CommandText = SQL; CMD.Connection = CN; da = new SqlDataAdapter(CMD); DataSet ds = new DataSet(); da.Fill(ds); dt = ds.Tables[0]; CN.Close(); return dt; }
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) { TemplateDataField field1 = new TemplateDataField(); field1.Key = "TemplateColumn1"; field1.Header.Text = "Edition"; this.WebDataGrid1.Columns.Add(field1); }
TemplateDataField templateColumn1 = (TemplateDataField)this.WebDataGrid1.Columns["TemplateColumn1"]; templateColumn1.ItemTemplate = new CustomItemTemplate();
this.WebDataGrid1.DataSource = Table(); this.WebDataGrid1.DataBind();
}
When you have editing you have to change:
to:
if (Session["data"] == null){ Session["data"] = Table();}
this.WebDataGrid1.DataSource = Session["data"];this.WebDataGrid1.DataBind();