2014-05-05 244 views
0

我想从网格中删除记录,但不是从数据库中删除记录。从gridview删除记录,但不是从数据库中删除记录

我想设置数据库字段ISDeleted 1当数据从gridview删除,但不想从数据库删除记录。

我的代码从gridview和db中删除记录。

凡在我的代码 -

string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString; 
SqlCommand command; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    tblAdd.Visible = false; 
    Label1.Visible = false; 
    //GridView1.DataBind(); 
    if (!Page.IsPostBack) 
     { 
      fillLanguageGrid(); 
     } 
} 

public void fillLanguageGrid() 
    { 
     GridView1.DataSourceID = "SqlDataSource1"; 
     GridView1.DataBind(); 
    } 

protected void btnDelete_Click(object sender, EventArgs e) 
{ 
    foreach (GridViewRow gvrow in GridView1.Rows) 
    { 
     CheckBox chkdelete = (CheckBox)gvrow.FindControl("chk"); 
     if (chkdelete.Checked) 
     { 
      string name= Convert.ToString(GridView1.DataKeys[gvrow.RowIndex].Values["Name"].ToString()); 
      // command.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50)); 
      deleteRecordByName(name); 
     } 
    } 
    fillLanguageGrid(); 
} 

public void deleteRecordByName(string Name) 
{ 
    SqlConnection sqlConnection = new SqlConnection(strcon); 
    using (SqlCommand command = new SqlCommand("[dbo].[hrm_Langauges]", sqlConnection)) 
    { 
     // define this to be a stored procedure 
     command.CommandType = CommandType.StoredProcedure; 
     command.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50)); 
     // define the parameter and set its value 
     command.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)).Value = Name; 
     command.Parameters.Add(new SqlParameter("@IsDeleted", SqlDbType.Bit)).Value = 1; 
     command.Parameters["@status"].Value = "Delete"; 
     //open connection, execute DELETE query, close connection 
     sqlConnection.Open(); 
     command.ExecuteNonQuery(); 
     sqlConnection.Dispose(); 
    } 

} 

回答

1

对于您需要在各自的数据库表中添加一列是否显示记录或not.For防爆改变:增加柱像可见INT

假设如果

可见= 1 - >显示在GridView的那个记录

可见= 0 - >隐藏在GridView的那个记录

作出缺席可见= 1因此所有记录显示在gridview中(写入查询如选择...... Where Visible = 1)。当您尝试删除记录时使用更新查询需要t O更新可见1比0。所以你的GridView只显示记录,其中可见= 1。那特定的删除记录未在你的GridView显示,因为它可见列0.Try这个..