472,795 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,795 software developers and data experts.

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 1888
Sorry for bump, but any ideas?
Jul 8 '08 #2

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

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
0
by: Dorte | last post by:
Hi, I use the gridview control to display data. I use a dataadapter to fill a datatable from an SQL server - the command is a stored procedure. Furthermore I have a couple of template columns...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
3
by: Carlos | last post by:
Hi all, I currently have a Gridview that doess sorting correctly using several fields. However, I do have a checkbox field that when it is checked I do not know how to make its value to persist...
6
by: CSharpguy | last post by:
In my gridview I have 2 -3 template fields which are hyperlinks. I allow the user to print this grid. When the grid prints it also prints the links, how can I take the user to a print preview page...
4
by: Tomasz Jastrzebski | last post by:
Hello Everyone, I have a GridView control bound to a plain DataTable object. AutoGenerateEditButton is set to true, Edit button gets displayed, and RowEditing event fires as expected.
4
by: beton3000 | last post by:
Hello! I'm building a GridView using code to add template fields, because there is a random number of columns in result set from database. I'm using a class with ITemplate interface for defining...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a gridview with several template columns and a few command button columns, column 1 and 2 are dropdown boxes, column 3 is a checkbox and column 4 is a text box, followed by command button...
1
by: renuami | last post by:
Hello friends please advise....... I am building an application for Survey. For this i have GridView with two Template fields. The first Template has Label (To display question text) and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.