Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am having a editable gridview which is populated on the click of "Load Data" button.

For saving the edited values in the grid, there is another button called "Save" on the same page.

On clicking save button, the page is getting posted back and gridview values are getting cleared, due to which i am not able to save the latest data.

I am working on ASP.Net 4.0

Any help would be greatly appreciated.

Code of .cs file --

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        populateDropDown();
    }
}

protected void btnLaunchTour_Click(object sender, EventArgs e)
{
    try
    {
        ///Code for populating Grid on Launch Button Click
        populateGrid(TourLaunchDAL.LaunchTour(0, Convert.ToInt32(ddlProduct.SelectedValue), Convert.ToInt32(txtNoOfTours.Text), UserDetails.UserID));
    }
    catch (Exception ex)
    {
        ErrorMessage = ex.Message;
    }

}

protected void populateGrid(List<TourLaunchBAL> lstTourSchedule)
{
    grdTourSchedule.DataSource = lstTourSchedule;
    grdTourSchedule.DataBind();
}

protected void populateDropDown()
{
    ListItem lstItem = new ListItem("Select", "0");

    ddlProduct.AppendDataBoundItems = true;
    ddlProduct.Items.Add(lstItem);

    ddlProduct.DataSource = CacheMaster.getProductsForLaunch();
    ddlProduct.DataTextField = "ProductName";
    ddlProduct.DataValueField = "ProductID";
    ddlProduct.DataBind();
}
protected void grdTourSchedule_RowCommand(object sender, GridViewCommandEventArgs e)
{

}
protected void grdTourSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
{

}

protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvRow in grdTourSchedule.Rows)
    {

    }
}





Thanks
Posted
Updated 8-Mar-18 1:01am
v2
Comments
Thanks7872 19-Sep-13 5:09am    
On Save button click,commit changes into database and then bind gridview again.
Madhu Nair 19-Sep-13 5:39am    
On Save button click, i am not getting any rows of gridview
Thanks7872 19-Sep-13 5:47am    
Use Improve question and post the relevant code for save button,page_load.
Madhu Nair 19-Sep-13 5:57am    
i am not populating grid on page load... but on button click... again on save button click i am trying to save data... but not getting any rows
Dholakiya Ankit 19-Sep-13 5:41am    
have u added
if(!page.ispostback)
{}
at page load?

1 solution

Managed to Solved the issue by making the following changes in the code--

protected void populateGrid(List<tourlaunchbal> lstTourSchedule)
{
grdTourSchedule.EnableViewState = true;

grdTourSchedule.DataSource = lstTourSchedule;
grdTourSchedule.DataBind();
ViewState["lstTourSchedule"] = lstTourSchedule;
//grdTourSchedule.EnableViewState = true;

}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populateDropDown();
}

if (grdTourSchedule.Rows.Count == 0)
{
grdTourSchedule.DataSource = (List<tourlaunchbal>)ViewState["lstTourSchedule"];
grdTourSchedule.DataBind();
}
}
Now i am able to get modified rows also.

Thanks for all the help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900