By David


2018-02-13 13:58:25 8 Comments

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

        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?

1 comments

@hankor 2018-02-13 15:21:51

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 e.g. button.Attributes.Add("data-person-id",somevalue).

Related Questions

Sponsored Content

35 Answered Questions

3 Answered Questions

[SOLVED] How to make dynamic column header clickable in gridview

1 Answered Questions

1 Answered Questions

How to set a colspan in an android gridview cell

6 Answered Questions

[SOLVED] Hide Autogenerated Column in Gridview

  • 2009-09-15 15:05:56
  • SchwartzE
  • 7023 View
  • 8 Score
  • 6 Answer
  • Tags:   asp.net gridview

1 Answered Questions

[SOLVED] Adding an edit button to Gridview

1 Answered Questions

Format Dynamically Generated Column Headers Of GridView

1 Answered Questions

[SOLVED] How do I use the RowCreated event for GridViews

0 Answered Questions

ASP.NET Two-Way Binding with GridView and Dynamic Templates

  • 2010-11-18 13:26:55
  • markpirvine
  • 1063 View
  • 3 Score
  • 0 Answer
  • Tags:   asp.net

Sponsored Content