Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid not visible when databind() on editcommand

Status
Not open for further replies.

j9

Programmer
Jun 6, 2001
90
0
0
US
I have a datagrid with an arraylist as the datasource. I added an Edit, Update, Cancel button column and coded the EditCommand event handler. When I click on the Edit button, the datagrid disappears. Have I lost my datasource during postbacks or something? Here's my code:

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
PopulateForm();
}
}

private void PopulateForm()
{
dgReferences.DataSource = mySOP.Refs;//(mySOP.Refs is an arraylist)
dgReferences.DataKeyField = "RefID";
dgReferences.DataBind();
}

private void dgReferences_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgReferences.EditItemIndex = e.Item.ItemIndex;
dgReferences.DataBind();
}
 
I would check what dgReferences.DataSource equals when the EditCommand event handler is called. If you have "lost my datasource during postbacks" you will notice that this will be null and therefore a call to PopulateForm would be needed when the page is posted back.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
My fix: I put the arraylist in a session variable and then reset the datasource in the EditCommand event handler. I was trying to avoid calling populateform() because it does a lot of other things (calls to db, etc.). Does this sound like an OK way to do this or am I going to get myself into trouble?
 
If you simply want to do the things that you do in your original post for the databind, why not strip those out and place them in their own function?

Then you can place the call to this new function in both the PopulateForm function and in the EditCommand function.

e.g.
Code:
private void NewFunction()
{    
    dgReferences.DataSource = mySOP.Refs;//(mySOP.Refs is an arraylist)
    dgReferences.DataKeyField = "RefID";
    dgReferences.DataBind();
}

private void PopulateForm()
{   
    '.... Some other code for this function
    NewFunction() ' Call to the new function
    .... Some more code for this function
}

Hope that made sense!

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top