I have a gridview and 2 buttons outside it. On button click all rows of gridview should become editable. I am using ITemplate class achive this task, but unable to complete it.
below is my code to get editable gridview:
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string columnNameBinding;
private string controlType;
public GridViewTemplate(DataControlRowType type, string colname, string colNameBinding, string ctlType)
{
templateType = type;
columnName = colname;
columnNameBinding = colNameBinding;
controlType = ctlType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
if (controlType == "Label")
{
Label lb = new Label();
lb.ID = "lblName";
lb.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(lb);
}
else if (controlType == "TextBox")
{
TextBox tb = new TextBox();
tb.ID = "txtWeightage" + columnNameBinding;
tb.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(tb);
}
default:
break;
}
}
public void OnDataBinding(object sender, EventArgs e)
{
object bound_value_obj = null;
Control ctrl = (Control)sender;
IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
bound_value_obj = DataBinder.Eval(data_item_container.DataItem, columnName);
switch (templateType)
{
case DataControlRowType.Header:
Label lb = (Label)sender;
lb.Text = bound_value_obj.ToString();
break;
case DataControlRowType.DataRow:
TextBox txtbox = (TextBox)sender;
txtbox.Text = bound_value_obj.ToString();
break;
}
}
}