Here is the code. See my comments below in grid_RowUpdating. Thanks, Keith
protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ComID = grid.DataKeys[e.RowIndex].Value.ToString();
/*
This is where I am having the problem
grid.Rows[e.RowIndex].Cells.count is 1, even though there are 14
columns.
the referenced column is the command field for edit. There is no
reference to any other columns.
It appears that the NewValues property is not being
automatically populated with the name/value pairs of the revised non-key
fields prior to raising the grid-rowUpdating event.
*/
BuildColumnsDynamically();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
InitializeComponent();
}
}
private void InitializeComponent()
{
this.Init += new System.EventHandler(this._Default_Init);
this.Load += new System.EventHandler(this.Page_Load);
CommandField cf = new CommandField();
cf.ShowEditButton = true;
grid.Columns.Add(cf);
BuildColumnsDynamically();
}
private void BuildColumnsDynamically()
{
DataTable dt = ComponentsAccess.GetEnvComMatrix();
int colcount = 0;
foreach (DataColumn col in dt.Columns)
{
if (colcount > 1)
{
//Declare the bound field
TemplateField chkColumn = new TemplateField();
chkColumn.HeaderTemplate = new
GridViewTemplate(ListItemType.Header, col.ColumnName);
chkColumn.ItemTemplate = new
GridViewTemplate(ListItemType.Item, col.ColumnName);
chkColumn.EditItemTemplate = new
GridViewTemplate(ListItemType.EditItem, col.ColumnName);
//Add the newly created bound field to the GridView.
grid.Columns.Add(chkColumn);
}
colcount++;
}
//Initialize the DataSource
grid.DataSource = dt;
//Bind the datatable with the GridView.
grid.DataBind();
}
public class GridViewTemplate : System.Web.UI.Page, ITemplate
{
ListItemType templateType;
string columnName;
public GridViewTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
LinkButton lb = new LinkButton();
CheckBox ckh = new CheckBox();
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
Label lb1 = new Label();
switch (templateType)
{
case ListItemType.Header:
lc.Text = columnName;
container.Controls.Add(lc);
break;
case ListItemType.Item:
lb1.DataBinding += new EventHandler(lb_DataBinding);
lb1.Text = columnName;
lb1.Width = 5;
container.Controls.Add(lb1);
break;
case ListItemType.EditItem:
tb2.DataBinding += new EventHandler(tb_DataBinding);
tb2.Text = columnName;
tb2.Columns = 5;
container.Controls.Add(tb2);
break;
}
}
void tb_DataBinding(object sender, EventArgs e)
{
TextBox txtdata = (TextBox)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, columnName);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}
}
void lb_DataBinding(object sender, EventArgs e)
{
Label txtdata = (Label)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, columnName);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}
}
Ken Cox - Microsoft MVP said:
Hi Keith,
There's certainly a way to do what you want.
It would help if you showed the code that's causing problems so we could
analyze it and probably fix it.
Ken
Microsoft MVP [ASP.NET]
keithb said:
My code dynamically adds template fields to a GridView control.
Everything seems to work OK, except when updating, because I haven't
found a way to reference the dynamically added textboxes. FindControl
doesn't work, and the textboxes cannot be accessed using
grid.Rows[e.RowIndex].Cells[x].Control[x]. Can someone help?
Thanks,
Keith