470,849 Members | 1,253 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Persisting Values in a Gridview using Template Columns

Hello!

I'm dynamically creating a GridView bound to a datatable using custom template columns using the following method:

Expand|Select|Wrap|Line Numbers
  1.         protected void ShowGrid(DataTable dt)
  2.         {
  3.             int i;
  4.             int j;
  5.             for( i = 0; i < dt.Columns.Count; i++)
  6.             {
  7.                     TemplateField tf = new TemplateField();
  8.                     tf.HeaderTemplate = new GridViewTemplate(ListItemType.Header, dt.Columns[i].ColumnName);
  9.                     for(j = 0; j < dt.Rows.Count; j++)
  10.                     {
  11.                         tf.ItemTemplate = new GridViewTemplate(ListItemType.EditItem, dt.Columns[i].ColumnName + j.ToString(),GridView1.Rows[i].Cells[j].Controls[0].ToString());
  12.                     }
  13.                     GridView1.Columns.Add(tf);
  14.             }
  15.         }
Using a custom class called GridViewTemplate which extends ITemplate I create a template field depending on the datatable column name:

Expand|Select|Wrap|Line Numbers
  1.           switch (templateType)
  2.             {
  3.  
  4.                 ...
  5.  
  6.                 case ListItemType.EditItem:
  7.  
  8.                     TextBox tb = new TextBox();
  9.                     tb.ID = columnName;
  10.  
  11.                     if (columnName.Contains("Date"))
  12.                     {
  13.                         CalendarExtender cal = new CalendarExtender();
  14.                         cal.TargetControlID = tb.ID;
  15.                         cal.Format = "MM/dd/yy";
  16.                         cal.PopupPosition = CalendarPosition.BottomLeft;
  17.                         tb.Text = value;
  18.                         container.Controls.Add(cal);
  19.                         container.Controls.Add(tb);
  20.                     }
  21.                     else if (columnName.Contains("Usage"))
  22.                     {
  23.                         Label l = new Label();
  24.                         l.Text = " kWh";
  25.                         tb.Width = 150;
  26.                         tb.Text = value;
  27.                         container.Controls.Add(tb);
  28.                         container.Controls.Add(l);
  29.                     }
  30.                     else if (columnName.Contains("Cost"))
  31.                     {
  32.                         tb.Text = value;
  33.                         container.Controls.Add(tb);
  34.                     }
  35.                     break;
I am unable to persist the Gridview textbox values across a page refresh. (such as when adding rows to the gridview). I have read that the best method to do this is to handle the RowDataBound event of the gridview so mine is as follows:

Expand|Select|Wrap|Line Numbers
  1.    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  2.         {
  3.             TextBox txt;
  4.             if (e.Row.RowType == DataControlRowType.DataRow)
  5.             {
  6.                 for (int i = 0; i < e.Row.Cells.Count; i++)
  7.                 {
  8.                     for (int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
  9.                     {
  10.                         if (e.Row.Cells[i].Controls[j] is TextBox)
  11.                         {
  12.                             txt = (TextBox)e.Row.Cells[i].Controls[j];
  13.  
  14.                             if (txt != null && Page.IsPostBack)
  15.                             {
  16.                                 txt.Text = Request.Form[txt.UniqueID];
  17.                             }
  18.                         }
  19.                     }
  20.                 }
  21.             }
  22.         }
Unfortunately this does not work at all. any ideas on how i can maintain the values entered into my textboxes in my gridview?
Jul 4 '08 #1
1 1806
Sorry for bump, but any ideas?
Jul 8 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by NateDawg | last post: by
3 posts views Thread by Carlos | last post: by
6 posts views Thread by CSharpguy | last post: by
4 posts views Thread by Tomasz Jastrzebski | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.