Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
This is the code i used to bind a grid and delete records on click..
on clicking the delete button items get delete from the datatbase but the gridview is not populate with correct data(Deleted item also seen in the items) what is the pbm with my code...please fuide me in htis...Tnaks for all help in advance

ASP.NET
<asp:GridView ID="grdLocation" runat="server" AutoGenerateColumns="False" DataKeyNames="LocationID"
             OnRowCancelingEdit="grdLocation_RowCancelingEdit"
             OnRowDeleting="grdLocation_RowDeleting" OnRowEditing="grdLocation_RowEditing" 
             OnRowUpdating="grdLocation_RowUpdating" Visible="False" 
                    onrowdatabound="grdLocation_RowDataBound1">
                <columns>
                <asp:BoundField DataField="LocationID" HeaderText="Category ID" Visible="False" />
                <asp:BoundField DataField="LocationName" HeaderText="Category Name" />
                <asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
                </columns>


C#
public void bindgrdLocation()
       {
           grdLocation.DataSource = (new Locations()).GetAllLocations();
           grdLocation.DataBind();
           if (grdLocation.Rows.Count > 0)
           {
               grdLocation.Visible = true;
               lblMessage.Visible = false;
           }
           else
           {
               grdLocation.Visible = false;
               lblMessage.Visible = true;
               lblMessage.Text = "No records Found";
           }
       }

 protected void grdLocation_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           Locations location = new Locations();
           location.LocationID = Convert.ToInt32(grdLocation.DataKeys[e.RowIndex].Value.ToString());
           location.Delete();
           bindgrdLocation();

       }
Posted
Updated 15-Jan-12 19:06pm
v3

Hi,

call the grid bind method "bindgrdLocation" again to bind the grid after the delete operation is performed on grdLocation_RowDeleting() event
 
Share this answer
 
Comments
amolpatil2243 16-Jan-12 1:32am    
no, i am simply firing the Sproc to delete the record and rebind the grid again
Are you passing the index of the row to be deleted??

Gv1.Deleterow(e.RowIndex);
Gvi.DataBind();
 
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