Frage

I have a GridView that gets a DataSource. Now on RowDataBound, I need to make a couple changes to the row cells, but I need an outside piece of information to determine what change occurs.

static void GridRowDataBoundProbables(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            if (!int.TryParse(cell.Text, out postNum)) continue;
            cell.CssClass += " postCell";
            cell.Add(new Panel { CssClass = (**isHarness** ? PostHarness : PostThoroughbred) + postNum });
            cell.Add(new Label { Text = postNum.ToString() });
        }
    }
}

I need the isHarness bool, which is available when I create and bind the grid. Also since the grid is created during a static WebMethod call I can't make it a global on the page.

How can I get the value of isHarness into this function? I thought I could create my own EventArgs that inherit from GridViewRowEventArgs but i still dont know how to actually get the bool in my new args...

Edit

isHarness is a bool determined at time that the DataSource is created, but is not a part of the DataSource

Below is a mock of the outer call:

[WebMethod]
public static AjaxReturnObject GetProbables(string token, string track, string race, string pool)
{
    Tote tote = new Tote(...);
    GridView grid = new GridView();
    grid.RowDataBound += GridRowDataBound;
    grid.DataSource = tote.GetDataSource(); //isHarness is available during creation of DataSource
    //Here tote.isHarness is available from property
    grid.DataBind();
}
War es hilfreich?

Lösung

The answer, as always, is "it depends".

We don't know much about your bool isHarness. What is the scope of this variable? Is it a member of the page? A member of the GridView? Is it part of the GridView's datasource?

If it is a public member of the page, from your method you could cast sender to a web control, and then traverse the parent hierarchy until you find the Page, and get it from there.

If it is a member of the GridView, just do the same and traverse the parent structure until you find the GridView.

EDIT

Thanks for posting more source. It looks like isHarness is a member of the tote object. If that's the case, you probably cannot get it in your DataBound event. tote is only a local variable, and it is out of scope and long gone by that point. You probably need to store that value elsewhere if you want to access it in your GridRowDataBound code.

For example, you could extend GridView into a custom class that has an IsHarness property, and set that value in your GetProbables function. Then you could access it in your GridRowDataBound event by casting sender appropriately.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top