Click here to Skip to main content
12,399,471 members (56,546 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
i generated a gridview ,using follwing class am creating template field dynamically...when am try to read grid data its not getting value...how i can read
each row data and add to a list inorder to save to db

// data passing for generating gridview

if (dsGraphToBind.Tables[0].Rows.Count > 0)
           {
               foreach (DataColumn col in dsGraphToBind.Tables[0].Columns)
               {
                   //if (col.ColumnName != "Stage Order" && col.ColumnName != "Distance")
                   //{
                       //Declare the bound field and allocate memory for the bound field.
                       TemplateField bfield = new TemplateField();
 
                       //Initalize the DataField value.
                       bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
 
                       //Initialize the HeaderText field value.
                       bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);
 
                       //Add the newly created bound field to the GridView.
                       grvBaseFareGraph.Columns.Add(bfield);
                   }
               //}
           } 
 

// class used for generating gridview...
public  class GridViewTemplate : ITemplate
    {
        //A variable to hold the type of ListItemType.
        ListItemType _templateType;
 
        //A variable to hold the column name.
        string _columnName;
 
        //Constructor where we define the template type and column name.
        public GridViewTemplate(ListItemType type, string colname)
        {
            //Stores the template type.
            _templateType = type;
 
            //Stores the column name.
            _columnName = colname;
        }
 
        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            switch (_templateType)
            {
                case ListItemType.Header:
                    //Creates a new label control and add it to the container.
                    Label lbl = new Label();            //Allocates the new label object.
                    lbl.Text = _columnName;             //Assigns the name of the column in the lable.
                    container.Controls.Add(lbl);        //Adds the newly created label control to the container.
                    break;
 
                case ListItemType.Item:
                    //Creates a new text box control and add it to the container.
                    TextBox tb1 = new TextBox();                            //Allocates the new text box object.
                    tb1.DataBinding += new EventHandler(tb1_DataBinding);   //Attaches the data binding event.
                 
                    tb1.Columns = 4;                                        //Creates a column with size 4.
                    container.Controls.Add(tb1);                            //Adds the newly created textbox to the container.
                    break;
 
                case ListItemType.EditItem:
                    //As, I am not using any EditItem, I didnot added any code here.
                    break;
 
                case ListItemType.Footer:
                    CheckBox chkColumn = new CheckBox();
                    chkColumn.ID = "Chk" + _columnName;
                    container.Controls.Add(chkColumn);
                    break;
            }
        }
        /// <summary>
        /// This is the event, which will be raised when the binding happens.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tb1_DataBinding(object sender, EventArgs e)
        {
            TextBox txtdata = (TextBox)sender;
            txtdata.ID = "gtx";
            GridViewRow container = (GridViewRow)txtdata.NamingContainer;
            object dataValue = DataBinder.Eval(container.DataItem, _columnName);
            if (dataValue != DBNull.Value)
            {
 
                txtdata.Text = dataValue.ToString();
 
            }
          
        }
Posted 6-Jan-13 19:08pm
Updated 6-Jan-13 21:39pm
DaveAuld187.2K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100