W
Will
Hi all.
I'm trying to figure out how to design a WebForm app that may have
more than one bound DataGrid. What I want to accomplish is to create a
"helper" class that I can reuse for each of the DataGrids, as well as
in different projects.
So here is what I have come up with so far.
The first part of bound DataGrids I wanted to "consolidate" were some
of the events. For instance, lets say I have two grids dgDogs and
dgCats.
Each of the grids are editable. I want to find a way to reuse the
DataGrid events, so I created a helper class called
DataGridEventHelper. This class should hold at least the event classes
I want to reuse.
What I can't figure out how to do is DataBind() the datagrid from the
HelperClass. Once I figure out how to do this, then I think I can
extend the DataGridEventHelper class to do Lots Of Good Things. But
for now, it is just giving me a headache.
So, here is some of my code-behind for the aspx page and the code for
the helper class.
//----Code-behind code starts here
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
dgDogLoad();
}
}
public void dgDogLoad()
{
DataSet ds = new DataSet();
AnimalServices AS = new AnimalServices();
ds = AS.DogLookup(servername, database, "German Shepherd");
dgDog.DataSource = ds.Tables[0];
dgDog.DataBind();
}
public void dgCatLoad()
{
DataSet ds = new DataSet();
AnimalServices AS = new AnimalServices();
ds = AS.CatLookup(servername, database, "Russian Blue");
dgCat.DataSource = ds.Tables[0];
dgCat.DataBind();
}
private void InitializeComponent()
{
DataGridEventHelper dgeh = new DataGridEventHelper();
dgDog.EditCommand += new
DataGridCommandEventHandler(dgeh.DataGridEditCommand);
dgCat.EditCommand += new
DataGridCommandEventHandler(dgeh.DataGridEditCommand);
}
//----END OF Code-behind code
//----DataGridEventHelper code
public void DataGridEditCommand(object source,
DataGridCommandEventArgs e)
{
DataGrid dg = (DataGrid)source;
dg.EditItemIndex = e.Item.ItemIndex;
// This is what I can't figure out how to do.
// I need to DataBind() the datagrid I just came from.
// But, if I'm editing the dgDog grid, how do I call dgDog from this
code.
}
//----END OF DataGridEventHelper code
I though trying to "consolidate" the dgDogLoad and dgCatLoad code into
something like dgAnimalLoad that would be in the DataGridEventHelper
class and passing in some parameters like the AnimalServices.DogLoad
or AnimalServices.CatLoad, but I can't figure out how to do that
either.
If anyone has any advice, I'd appreciate it. Thanks.
I'm trying to figure out how to design a WebForm app that may have
more than one bound DataGrid. What I want to accomplish is to create a
"helper" class that I can reuse for each of the DataGrids, as well as
in different projects.
So here is what I have come up with so far.
The first part of bound DataGrids I wanted to "consolidate" were some
of the events. For instance, lets say I have two grids dgDogs and
dgCats.
Each of the grids are editable. I want to find a way to reuse the
DataGrid events, so I created a helper class called
DataGridEventHelper. This class should hold at least the event classes
I want to reuse.
What I can't figure out how to do is DataBind() the datagrid from the
HelperClass. Once I figure out how to do this, then I think I can
extend the DataGridEventHelper class to do Lots Of Good Things. But
for now, it is just giving me a headache.
So, here is some of my code-behind for the aspx page and the code for
the helper class.
//----Code-behind code starts here
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
dgDogLoad();
}
}
public void dgDogLoad()
{
DataSet ds = new DataSet();
AnimalServices AS = new AnimalServices();
ds = AS.DogLookup(servername, database, "German Shepherd");
dgDog.DataSource = ds.Tables[0];
dgDog.DataBind();
}
public void dgCatLoad()
{
DataSet ds = new DataSet();
AnimalServices AS = new AnimalServices();
ds = AS.CatLookup(servername, database, "Russian Blue");
dgCat.DataSource = ds.Tables[0];
dgCat.DataBind();
}
private void InitializeComponent()
{
DataGridEventHelper dgeh = new DataGridEventHelper();
dgDog.EditCommand += new
DataGridCommandEventHandler(dgeh.DataGridEditCommand);
dgCat.EditCommand += new
DataGridCommandEventHandler(dgeh.DataGridEditCommand);
}
//----END OF Code-behind code
//----DataGridEventHelper code
public void DataGridEditCommand(object source,
DataGridCommandEventArgs e)
{
DataGrid dg = (DataGrid)source;
dg.EditItemIndex = e.Item.ItemIndex;
// This is what I can't figure out how to do.
// I need to DataBind() the datagrid I just came from.
// But, if I'm editing the dgDog grid, how do I call dgDog from this
code.
}
//----END OF DataGridEventHelper code
I though trying to "consolidate" the dgDogLoad and dgCatLoad code into
something like dgAnimalLoad that would be in the DataGridEventHelper
class and passing in some parameters like the AnimalServices.DogLoad
or AnimalServices.CatLoad, but I can't figure out how to do that
either.
If anyone has any advice, I'd appreciate it. Thanks.