none
Asp.net GridView adding columns dynamically...

    Question

  • I have a GridView with 3 columns (Campus, Class and StudentName). these columns i have taken as a "TemplateField" with there header Text property, and "Literal" control as an "ItemTemplate" inside "TemplateField" for each column. Now at runtime i have added some new columns, number of columns are not defined, depends on the Class you selected. All the subjects of the selected "Class" with become as Columns which i add to GridView at runtime. at Its working fine... Here is my code...

    .aspx Page.....

    <asp:GridView ID="ExamGrid" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField HeaderText="Campus">
    <ItemTemplate>
    <asp:Literal ID="LiCampus" runat="server" Text='<%#Eval("CampusName") %>' />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Class">
    <ItemTemplate>
    <asp:Literal ID="LiClass" runat="server" Text='<%#Eval("ClassName") %>' />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Student">
    <ItemTemplate>
    <asp:Literal ID="LiStudent" runat="server" Text='<%#Eval("StudentName") %>' />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    Now the Columns that i have added on Runtime in .cs Code behind...

    ClsStudents Student = new ClsStudents();
    ExamGrid.DataSource = null;
    ExamGrid.DataSource = Student.GetStudents(Convert.ToInt32(DDLClass.SelectedValue));

    I have a class "ClsStudents", having method "GetStudents" which takes "ClassId" as a parameter, and according to the selected Class list of Student will be bind to "GridView" having CampusName,ClassName and StudentName three Columns...

    Now i have and other Class "ClsStudentSubjects", with method "GetSubjectsByClass" which also accepts "ClassId" as a parameter and returns the list of all subjects of the selected class... 

    ClsStudentSubjects Subject = new ClsStudentSubjects();
    List<ClsStudentSubjects> myList = new List<ClsStudentSubjects>();
    myList = Subject.GetSubjectsByClass(Convert.ToInt32(DDLClass.SelectedValue));

    Now i process the list and add each subjects two colums e.g if i have a subject "Software Engineering", then in GridView i have added two columns, "Software Engineer T-Marks" (Total Marks) and "Software Engineer O-Marks" (Obtained Marks) and so on for each subject.. In T-Marks Column i have added "Literal Control" while in "O-Marks" Column i have added "Textbox Control". I have my own Classes for adding Literal and TextBox Controls to GridView at runtime..

    int _index = 1;
    foreach (var sub in myList)
    {
    string _controlId = "SubTM" + _index;

    TemplateField literalField = new TemplateField();
    literalField.HeaderText = sub.SubjectName + " T-Marks";
    literalField.ItemTemplate = new myLiteral(_controlId, sub.SubjectTotalMarks.ToString());
    ExamGrid.Columns.Add(literalField);

    _controlId = "SubOM" + _index;
    TemplateField textBoxField = new TemplateField();
    textBoxField.HeaderText = sub.SubjectName + " O-Marks";
    textBoxField.ItemTemplate = new myTextBox(_controlId, "0");
    ExamGrid.Columns.Add(textBoxField);
    }
    ExamGrid.DataBind();

    //Literal Control Class...

    class myLiteral : ITemplate
    {
    private string ControlName;
    private string TextValue;
    //Constructor...
    public myLiteral(string _ControlId, string _TextValue)
    {
    ControlName = _ControlId;
    TextValue = _TextValue;
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
    Literal li = new Literal();
    li.ID = ControlName;
    li.Text = TextValue;
    container.Controls.Add(li);
    }

    }

    // TextBox control Class...

    class myTextBox : ITemplate
    {
    private string ControlName;
    private string TextValue;
    //Constructor...
    public myTextBox(string _ControlId, string _TextValue)
    {
    ControlName = _ControlId;
    TextValue = _TextValue;
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
    TextBox txt = new TextBox();
    txt.ID = ControlName;
    txt.Text = TextValue;
    txt.Width = Unit.Pixel(50);
    container.Controls.Add(txt);
    }
    }

    Up to that everything is working fine....

    The problem starts when i assign marks to each subjects obtainedMarks column of GridView, click Save button for saving data in database. So the two controls "Literal" and "TextBox" disappears, while the three columns does not disappears which where added from .aspx page, and the controls added from .cs page disappears on post back...

    I am doing it in this way, because in each class i have different number of subjects...

    How should i save the result, any help....

    Thursday, February 28, 2013 6:36 PM

Answers