I'm using a form to populate a gridview with x users. However I want whenever I call the function if there is only 1 user displayed inside the gridview to autoselect that first user.
So within my callback function I have
if (users.count == 1)
{
// Do something
}
I currently use the following function upon someone pushing "select" alongside the gridview.
Users_SelectedIn开发者_如何学编程dexChanged(object sender, EventArgs e)
It would be nice if I could reuse this function and do something like
if (users.count == 1)
{
Users_SelectedIndexChanged(object sender, EventArgs e);
}
Use the GridView.SelectedIndex
property.
Setting this property to 0
after binding the data and checking for at least one item ought to do the trick. Technically you could call the event handler method, but to no end other than executing your own implementation.
I would use the GridView's OnLoad method and call your function there. Data has been bound when the OnLoad method is called.
public void GridView_OnLoad(object sender, EventArgs e)
{
//Assuming one row means 1 user and gv is your gridview object
if (gv.Rows.Count == 1) //(user.count == 1)
{
//call your selected function here
}
}
精彩评论