I have a drop down list that fetches the data for the Products or the Order table into a dataset. This dataset gets bounded to a gridview. The data in the gridview has to be editable. I was thinking of dynamically creating itemtemplates w/ textboxes depending on the number of columns in the dataset.
I've been using this link as a reference: But I can't figure out how the InstantiateIn method gets called. After stepping thru the code, the InstantiateIn method runs after the page load even. But again I don't see code that calls this method.
Any help would be appreciated.
This is what I have so far:
I've been using this link as a reference: But I can't figure out how the InstantiateIn method gets called. After stepping thru the code, the InstantiateIn method runs after the page load even. But again I don't see code that calls this method.
Any help would be appreciated.
This is what I have so far:
Code:
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate(DataControlRowType type, string colName)
{
templateType = type;
columnName = colName;
}
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:
TextBox txtMapping = new TextBox();
txtMapping.DataBinding += new EventHandler(this.Mapping_DataBinding);
container.Controls.Add(txtMapping);
break;
default:
break;
}
}
private void Mapping_DataBinding(Object sender, EventArgs e)
{
TextBox t = (TextBox)sender;
GridViewRow row = (GridViewRow)t.NamingContainer;
t.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGridView();
}
}
private void LoadGridView()
{
Database SqlDb = DatabaseFactory.CreateDatabase();
DataSet dsMappingTable = SqlDb.ExecuteDataSet(CommandType.Text, "SELECT * FROM " + ddlMappingTable.Items[ddlMappingTable.SelectedIndex].Value.ToString());
gvMappingTable.DataSource = dsMappingTable.Tables[0];
gvMappingTable.DataBind();
for (int i = 1; i < dsMappingTable.Tables[0].Columns.Count; i++)
{
TemplateField tf = new TemplateField();
tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, dsMappingTable.Tables[0].Columns[0].ColumnName);
tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, dsMappingTable.Tables[0].Columns[0].ColumnName);
gvMappingTable.Columns.Add(tf);
}
}
protected void ddlMappingTable_SelectedIndexChanged(object sender, EventArgs e)
{
LoadGridView();
}
}