473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1922
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.