Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

C#
// 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
Updated 6-Jan-13 21:39pm
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900