0

Follow up from a previous question I have made: Getting and error I cannot fix when trying to add a button column

After making the aforementioned corrections given by the accepted answer, my code will now build successfully. However, neither the column header or the buttons will show up. Is there someplace I should be adding the column specifically? My code for generating the table looks like such:

void LoadDataTable()
    {
        //create table
        DataTable table = new DataTable();
        AddColumnToTable(table);

        //query all students
        IQueryable<Member> query = from mem in SLHS_DB.Members
                                       where mem.RoleId == (int)Credentials.MemberRole.STUDENT
                                       select mem;

        Member[] students = query.ToArray();

        //add them to table
        curStudentIndex = 0;

        foreach (Member student in students)
        {
            AddRowToTable(table, student);
        }


        //bind data to grid view
        gridViewStudent.DataSource = table;
        gridViewStudent.DataBind();

        var btn = new ButtonField();
        btn.HeaderText = "Click Data";
        btn.Text = "Click Here";

        gridViewStudent.Columns.Add(btn);
    }

And the part where I attempt to add a column of buttons are the last few lines. I have tried adding a new column beforehand in the local function AddColumnToTable():

void AddColumnToTable(DataTable table)
    {
        table.Columns.Add(NUMBER    , typeof(int));
        table.Columns.Add(FIRSTNAME , typeof(string));
        table.Columns.Add(LASTNAME  , typeof(string));
        table.Columns.Add(AGE       , typeof(int));
        table.Columns.Add(EMAIL     , typeof(string));

        //table.Columns.Add(EDIT      , typeof(ButtonField));
    }

where the commented part was what I tried, but it made no impact.

Sohee
  • 23
  • 5
  • `gridViewStudent.DataSource` & `gridViewStudent.DataBind` should be placed after `gridViewStudent.Columns.Add` in `RowDataBound` event. In which event that `AddColumnToTable` & `LoadDataTable` method bounds to? – Tetsuya Yamamoto Sep 06 '17 at 02:58
  • Are you saying that I should re-order then in the way you mentioned and then put them in the function that generates the rows? Also I am not sure what you mean by which events AddColumnToTable and LoadDataTable are bounded to. They should just automatically start up generating the table as soon as I load the web page that it is on correct? – Sohee Sep 06 '17 at 03:40
  • I think you're adding `ButtonField` after the data binding with `DataBind` which doesn't makes sense (try reordering with `DataBind` at last). What I want to find out is in which `GridView` event that both 2 methods above was called (`RowDataBound`, `Init`, `Load` or event methods like that). – Tetsuya Yamamoto Sep 06 '17 at 03:44
  • Could it possible be this? **protected void Page_Load(object sender, EventArgs e) { CheckMember(); LoadDataTable(); }** Sorry about the formatting I am not sure how to format code in comments – Sohee Sep 06 '17 at 04:12
  • Yes, the `Page_Load` method matches as one of the event methods I said before. I think you can't add a `ButtonField` into `DataTable` - it accepts only value types & strings, `ButtonField` instance must be added separately in `RowDataBound` or `GridView` column-related event methods. – Tetsuya Yamamoto Sep 06 '17 at 04:17
  • So then which event must I put it in if not into a Load event? Shouldn't I want this column to load once the page is loaded? – Sohee Sep 06 '17 at 04:25
  • You can try creating `ButtonField` in `RowDataBound` event if `Load` event binding doesn't work. Your problem seems similar to these: https://stackoverflow.com/questions/30229127/add-gridview-button-dynamically & https://stackoverflow.com/questions/15839399/how-to-store-buttonfield-in-a-datarow-where-the-column-is-set-for-buttonfield (note the use of `TemplateField`). – Tetsuya Yamamoto Sep 06 '17 at 04:39

0 Answers0