Question : Dynamic  textbox template column in gridview

Hi,

How to create dynamic  textbox template column in gridview
using C# .net?
How to retrieve value entered textbox template column?

Actually i'm able come up with dynamic textbox template column in gridview.
However when I’m trying to findcontrol method to retrieve value entered textbox template column,
'Object reference not set to an instance of an object.' error occurred.
Refer Attached  code

Pls help.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
public void LoadColumn()
    {
        BoundField nameColumn = new BoundField();
        nameColumn.DataField = "NAME";
        nameColumn.HeaderText = "Name";
        gvTitle0.Columns.Add(nameColumn);
        nameColumn = new BoundField();
        nameColumn.DataField = "EMPLID";
        nameColumn.HeaderText = "Emplid";
        gvTitle0.Columns.Add(nameColumn);

        OracleConnection con = null;
        OracleDataReader dtr = null;
        con = Util.openConn();

        try
        {
            string sql = string.Empty;

            sql = "SELECT  A.PORTION  , A.DEPT_ID FROM PRO_COST A WHERE A.DEPT_ID = '" + drpDept.SelectedValue.ToString() + "'";

            Log.Write("LoadColumn sql: " + sql);
            if (con != null)
            {
                OracleCommand cmd = con.CreateCommand();
                cmd = new OracleCommand(sql, con);
                cmd.CommandType = CommandType.Text;
                dtr = cmd.ExecuteReader();
                ColumnID = 1;
                if (dtr.HasRows)
                {
                    while (dtr.Read())
                    {
                        TemplateField TmpCol = new TemplateField();
                        TmpCol.HeaderText = dtr["PORTION"].ToString();
                        gvTitle0.Columns.Add(TmpCol);
                        
                        TmpCol.ItemTemplate = new TemplateHandler();
                        

                    }
                }

                dtr.Dispose();
                dtr.Close();
            }

            con.Dispose();
            con.Close();

        }
        catch (Exception ex)
        {
            con.Dispose();
            con.Close();
            Log.Write("Error LoadColumn: " + ex.Message.ToString() + "\n" + ex.StackTrace);
        }
    }
	
	 public class TemplateHandler : ITemplate
    {
        void ITemplate.InstantiateIn(Control container)
        {
          
            TextBox cmd = new TextBox();
            cmd.ID = "gvs" + ColumnID.ToString();
            Log.Write("ColumnID: " + cmd.ID);
            cmd.Text = "0";
            container.Controls.Add(cmd);
            ColumnID++;
        }
        
    }

Answer : Dynamic  textbox template column in gridview

Are you re-loading the column after each post-back?  If you aren't, then it would probably explain that error.  All dynamic controls need to be re-loaded every time.
Random Solutions  
 
programming4us programming4us