Click here to Skip to main content
16,019,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my application i m use grid view to update record. the data get updated in db. but in grid view show last record and when i m update the last record it get updated in db.but i don't want to display updated record in grid view. below i m write code u will get idea what i want

source code

XML
<asp:GridView  AutoGenerateColumns ="false" BorderWidth="1px" BorderStyle="None"
        CellPadding="3" ID ="gridtitle" Width="100%" runat ="server"
 OnRowDataBound="grd_title" OnRowCommand="delete"   RowStyle-HorizontalAlign="Center" >
<RowStyle HorizontalAlign="Center"></RowStyle>
<Columns>
<asp:TemplateField HeaderText="Title">
<HeaderStyle Height="18px" HorizontalAlign="center"/>
<ItemStyle  Width="20%" HorizontalAlign="center" />
<ItemTemplate>
<asp:Label ID ="lbl_title"  runat ="server" text='<%#Bind("content_heading")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<HeaderStyle Height="18px" HorizontalAlign="center"/>
<ItemStyle  Width="20%" HorizontalAlign="center" />
<ItemTemplate>
<asp:Button ID ="btn1"  CommandArgument = '<%#Bind("page_id")%>' CommandName = '<%#Bind("div")%>'    runat ="server" text ="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>




.cs

public partial class Edit_Home_Page : System.Web.UI.Page
{
    DataSet ds = new DataSet("temp");
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            bindgrid();
        }
      
      
    }




    public void bindgrid()
    {
        try
        {
            ds.Tables.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString;
            SqlCommand cmd = new SqlCommand("select content_heading,Page_id,div from content_managment where page_id = 1 and status != 'deleted'", con);

            SqlDataAdapter ad = new SqlDataAdapter();
            ad.SelectCommand = cmd;
            ad.Fill(ds);
            
            if (ds != null)
            {

               
                
                    if (ds.Tables[0].Rows.Count >0)
                    {

                        
                    

                       
                        gridtitle.DataSource = ds.Tables[0].DefaultView;
                        gridtitle.DataBind();
                      


                    }
                    else
                    {
                       
                    }
                
            }
        }
        catch (Exception)
        {
            Session["exception"] = "some error happen";
            throw;
        }
    }

    protected void delete(object sender, GridViewCommandEventArgs e)
    {
        //Mention here command name instead of "t1"
          
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString);
            con1.Open();
        int a = Convert.ToInt32(e.CommandArgument);
       int b = Convert.ToInt32(e.CommandName);
            string str1 = "update content_managment set status = 'deleted' where page_id = @page_id and div = @div_id";
            SqlCommand cmd1 = new SqlCommand(str1, con1);
            cmd1.Parameters.Add("@page_id", SqlDbType.Int);
            cmd1.Parameters.Add("@div_id", SqlDbType.Int);
            cmd1.Parameters["@page_id"].Value = a.ToString();
            cmd1.Parameters["@div_id"].Value = b.ToString();
            int updaterow = cmd1.ExecuteNonQuery();
            if (updaterow > 0)
            {

                Label1.Visible = true;
                Label1.Text = "Deleted Sucessfully";
                // Response.Redirect("Default2.aspx");
               // gridtitle.DataSource = ds.Tables[0].DefaultView; ;
               // gridtitle.DataBind();
               
            
            }
            else
            {
                 Label1.Visible = true;
                 Label1.Text = "Not Deleted";
            }
            bindgrid();

        }



       protected void grd_title(object sender, GridViewRowEventArgs e)
    {
      /*  if (e.Row.RowType.Equals(DataControlRowType.DataRow))
        {
            Label lbl_title = new Label();
            lbl_title = (Label)e.Row.FindControl("lbl_title");
            lbl_title.Text = ds.Tables[0].Rows[e.Row.RowIndex][0].ToString();
        }*/
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button b = (Button)e.Row.FindControl("btn1");
            b.Attributes.Add("onclick", "javascript:return " +
             "confirm('Are you sure you want to delete this record " +
             DataBinder.Eval(e.Row.DataItem, "content_heading") + "')");

        }
    }

    
}
Posted

C#
Gridview1.Datasource=ds;
Gridview1.DataBind();



i hope it works...
 
Share this answer
 
Comments
Sandeep Mewara 11-Jun-12 5:35am    
1. 'ds' is a dataset and assigning to it wont help.
2. Though your answer is not complete, I believe you want to say 'rebind' the grid.
Try to use EditItemTemplate with Item Template. like:

XML
<Columns>
<asp:TemplateField HeaderText="Title">
<HeaderStyle Height="18px" HorizontalAlign="center"/>
<ItemStyle  Width="20%" HorizontalAlign="center" />
<ItemTemplate>
<asp:Label ID ="lbl_title"  runat ="server" text='<%#Bind("content_heading")%>'></asp:Label>
</ItemTemplate>

<EditItemTemplate>
<asp:Label ID ="lbl_title1"  runat ="server" text='<%#Bind("content_heading")%>'></asp:Label>
</EditItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<HeaderStyle Height="18px" HorizontalAlign="center"/>
<ItemStyle  Width="20%" HorizontalAlign="center" />
<ItemTemplate>
<asp:Button ID ="btn1"  CommandArgument = '<%#Bind("page_id")%>' CommandName = '<%#Bind("div")%>'    runat ="server" text ="Delete" />
</ItemTemplate>


</asp:TemplateField>
</Columns>
 
Share this answer
 
Comments
Sandeep Mewara 11-Jun-12 5:33am    
This would not help as OP is just calling 'Delete' button and looks like he wants to remove the row once done.

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