简体   繁体   中英

How to make an autogenerated gridview with buttons in each dynamic column?

I need to create a gridview off a pivoted version of the following table layout: Person 1..N PersonDate N...1 Date: ie

        Person                       PersonDate            Date
   PersonID  Person            PersonID DateID Flag        DateID Date
   1         John              1        1      Y           1      01/01/2018
   2         Jane              1        2      N           2      02/01/2018
                               2        2      N

But I need to pivot this into a gridview, such that the gridview column structure looks like:

Person  01/01/2018  02/01/2018
John    Y           N
Jane                N

The columns are dynamically generated as more dates are added in. I've achieved this via use of a datatable. But I also need to add a button into each cell on the gridview's result to toggle the flag.

How can I insert a button into a column that isn't present until runtime? I thought about using onrowcreated, but then how do I distinguish between the buttons when one is clicked?

Alternatively, is there a better way to achieve this?

Assuming your resulting DataTable looks like this:

protected void Page_Load(object sender, EventArgs e)
{
    var table = new DataTable();
    table.Columns.Add("Person");
    table.Columns.Add("01/01/2018");
    table.Columns.Add("02/01/2018");
    table.Rows.Add("John", "Y", "N");
    table.Rows.Add("Jane", "N", "N");

    grvTest.DataSource = table;
    grvTest.RowDataBound += GrvTest_RowDataBound;
    grvTest.RowCommand += GrvTest_RowCommand;
    grvTest.DataBind();
}

you can use the RowDataBound event to add the buttons. To identify the person/date combination that the button was clicked for, you can set the necessary data as the CommandArgument of the buttons you add. The data could be the IDs of person and date entries. I have used the display values to keep the example simple:

private void grvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow)
        return;

    for (int i = 1; i < e.Row.Cells.Count; i++)
    {
        var row = (DataRowView)e.Row.DataItem;
        var currentPerson = row[0].ToString();
        var currentDate = row.DataView.Table.Columns[i];
        var commandArgument = currentPerson + ":" + currentDate;
        var button = new Button {Text = row[i].ToString(), CommandName = "Toggle", CommandArgument = commandArgument };
        e.Row.Cells[i].Controls.Add(button);
    }
}

In the RowCommand event you can then access that argument again:

private void GrvTest_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Response.Write(e.CommandArgument);
}

If you don't want to concatenate the two values you could also use an arbitrary additional attribute of the button eg button.Attributes.Add("data-person-id",somevalue).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM