数据更新后如何刷新gridview [英] how to refresh gridview after data get updated

查看:210
本文介绍了数据更新后如何刷新gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用网格视图更新记录.数据在db中更新.但是在网格视图中显示最后一条记录,当我更新最后一条记录时,它将在db.db中更新,但是我不想在网格视图中显示更新的记录.下面我写代码,你会知道我想要什么

源代码

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

<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





.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") + "'')");

        }
    }

    
}

推荐答案

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




我希望它能起作用...




i hope it works...


尝试将EditItemTemplate与Item Template一起使用.像:

Try to use EditItemTemplate with Item Template. like:

<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>


这篇关于数据更新后如何刷新gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆