Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic gridview

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
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:
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();
        }
}
 
Why would the number of columns vary?
You should be writing a stored procedure for each scenrio you need and bind the grid to the results. this way you know the columns you will be getting back
 
The columns would have vary among the tables. The scenario I have given is simpler to the one I'm dealing. There are about 12 tables that the user can insert/update data to.

The problem was with the logic in creating a new template and binding the gridview. The above code should've been

Code:
            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);
            }

            gvMappingTable.DataSource = dsMappingTable.Tables[0];
            gvMappingTable.DataBind();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top