Yes, of course, you can create a template column at runtime. For an example...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingtemplatesprogrammaticallyindatagridcontrol.asp (I've also included the code for a templated image column below). Looking at your code, it appears you are either auto-generating your columns or have defined them all in the html. You have not added any columns dynamically from what I can see. BTW, all the properties you are setting on the grid control you could set in the html. The code and html I supplied in my earlier message show exactly how to display an image from your web server in a template column on the grid. Another note, you will find your performance is better if you use the .NET SQL data objects http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclient.asp instead of the OLEDB objects. ---Code to add a templated image column dynamically at run time NOTE: text box in edit item could be used to edit the path, though you might make read only, by making it a label, or allow user to upload a file by using the HtmlFileInput control. private void AddTemplateColumnWithImageControl() { TemplateColumn tc = new TemplateColumn(); tc.HeaderTemplate = new DataGridImageTemplate(ListItemType.Header, "MyPic"); tc.ItemTemplate = new DataGridImageTemplate(ListItemType.Item, "MyPic"); tc.EditItemTemplate = new DataGridImageTemplate(ListItemType.EditItem, "MyPic"); tc.FooterTemplate = new DataGridImageTemplate(ListItemType.Footer, "MyPic"); DataGrid1.Columns.Add(tc); } using System; using System.Web.UI; using System.Web.UI.WebControls; public class DataGridImageTemplate : ITemplate { ListItemType templateType; string columnName; public DataGridImageTemplate(ListItemType type, string colname) { templateType = type; columnName = colname; } public void InstantiateIn(System.Web.UI.Control container) { Literal lc = new Literal(); switch(templateType) { case ListItemType.Header: lc.Text = "<B>" + columnName + "</B>"; container.Controls.Add(lc); break; case ListItemType.Item: Image img = new Image(); container.Controls.Add(img); break; case ListItemType.EditItem: TextBox tb = new TextBox(); tb.Text = ""; container.Controls.Add(tb); break; case ListItemType.Footer: lc.Text = "<I>" + columnName + "</I>"; container.Controls.Add(lc); break; } } } On 6/22/05, Michael Swanson <[EMAIL PROTECTED]> wrote: > Ok, I have had a few response on using the item template. However, > I have had to create almost everything in the datagrid at runtime. > The Image is basically stored as a url location on the webserver. > Will this item template work, and how do I use it with creating > these things at run time. Here is the code if that helps: > > namespace Custom > { > using System; > using System.Data; > using System.Data.OleDb; > using System.Drawing; > using System.Web; > using System.Web.UI.WebControls; > using System.Web.UI.HtmlControls; > > public class Products : System.Web.UI.UserControl > { > protected System.Web.UI.WebControls.DataGrid > dgProducts; > protected System.Data.OleDb.OleDbConnection > oleDbConnection1; > DataView dv; > > private void Page_Load(object sender, > System.EventArgs e) > { > oleDbConnection1.ConnectionString > = "Provider=SQLOLEDB;Data > Source=whsql03.prod.mesa1.secureserver.net,1433;Database=D" + > "B_9153;User > Id=creator;Password=yosef"; > OleDbCommand cmd = new OleDbCommand > ("spProducts", oleDbConnection1); > cmd.CommandType = > CommandType.StoredProcedure; > OleDbDataAdapter oda = new OleDbDataAdapter > (); > oda.SelectCommand = cmd; > DataSet ds = new DataSet(); > oda.Fill(ds, "tblItems"); > dv = new DataView(ds.Tables["tblItems"]); > dgProducts.AllowPaging = true; > dgProducts.PagerStyle.Mode = > PagerMode.NumericPages; > dgProducts.PagerStyle.PageButtonCount = 5; > dgProducts.PageSize = 5; > dgProducts.DataSource = dv; > dgProducts.AllowSorting = true; > > if (!Page.IsPostBack) > { > dgProducts.DataBind(); > } > } > > #region Web Form Designer generated code > override protected void OnInit(EventArgs e) > { > InitializeComponent(); > base.OnInit(e); > } > > private void InitializeComponent() > { > this.oleDbConnection1 = new > System.Data.OleDb.OleDbConnection(); > this.dgProducts.PageIndexChanged += new > System.Web.UI.WebControls.DataGridPageChangedEventHandler > (this.dgProducts_PageIndexChanged); > this.dgProducts.SortCommand += new > System.Web.UI.WebControls.DataGridSortCommandEventHandler > (this.dgProducts_SortCommand); > this.Load += new System.EventHandler > (this.Page_Load); > > } > #endregion > > private void dgProducts_PageIndexChanged(object > source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) > { > dgProducts.CurrentPageIndex = e.NewPageIndex; > dgProducts.DataBind(); > } > > private void dgProducts_SortCommand(object source, > System.Web.UI.WebControls.DataGridSortCommandEventArgs e) > { > dv.Sort = e.SortExpression; > dgProducts.DataBind(); > } > } > } > > > > > > > Yahoo! Groups Links > > > > > > > -- Dean Fiala Very Practical Software, Inc http://www.vpsw.com Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
