gh0st
gh0st

Reputation: 1714

Set an Elements Visibility Based on Cell Value of Bound Datatable

I'm trying to figure out a way in which a cell in a GridView is visible based on it's associated column in the bound DataTable. The issue I believe I'm having is that this table is dynamic and perhaps as rows are added to the table, the table is not being re-populated/re-freshed on the asp page.

Here is the scenario: I have a table, for example, of three columns and one row. Column 1 is the index, column 2 is a name, column 3 is true or false which is meant to set the visibility of a link in the asp page.

Row 1 is already set as 1, John Doe, false so on the asp page all you see is

1 | John Doe

You can hit a drop down, click a name, and then add this name to the datatable. By default column 3 is inserted with a true value. So after the row is inserted into the table the row is reflected on the asp page as so

1 | John Doe
2 | Jane Doe

But because column 3 is true, I would like a 'delete' button to be visible so the asp page would instead look something like this

1 | John Doe
2 | Jane Doe | Delete

Where 'Delete' is a link for deleting that newly inserted row.

I've found this thinking this is exactly what I need but the 'Delete' link still fails to display in the GridView.

What am I overlooking so that I can simply display an asp:LinkButton (or any link equivalent) based on a cell value in a DataTable?

Edit

Addition of RowDataBound eventhandler function.

protected void NamesGV_RowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        for (int i = 0; i < e.Row.Cells.Count; i++) {
            e.Row.Cells[i].BackColor = System.Drawing.Color.Beige; ;
        }
    }
}

Upvotes: 0

Views: 939

Answers (1)

mjw
mjw

Reputation: 1206

So something like this...not tested so you may need to tweak it a bit:

 protected void NamesGV_RowDataBound(object sender, GridViewRowEventArgs e) 
 {
    if (e.Row.RowType == DataControlRowType.Header) 
    {
        for (int i = 0; i < e.Row.Cells.Count; i++) 
        {
            e.Row.Cells[i].BackColor = System.Drawing.Color.Beige;
        }
    }

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         //Get reference to the button you want to hide/show
         LinkButton delButton = CType(e.Row.Cells(2).FindControl("lbYourLinkButtonId"), LinkButton);

         //check your data for value
         bool visible = (bool)DoACheckForAValue(NamesGV.DataKeys(e.Row.RowIndex).Value);
         delButton.Visible = visible;
    }
}

Upvotes: 1

Related Questions