从网格中删除记录而不是在数据库中删除 [英] deleting the record from grid and not in database
问题描述
hi
i有一个要求:
i有一个网格,它通过数据库绑定数据datasource现在我的要求是如果我点击删除按钮,它是通过网格中的 showdeletebutton = true 属性,只有记录应该从网格中删除而不是数据库中。
一次我点击完整按钮记录应该从数据库中删除...
请帮助
hi
i had one requirement it is like:
i have a grid which gets bind the data from database through datasource now my requirement is if i click on delete button which is through showdeletebutton=true property in the grid only the record should be deleted from grid and not in databse.
once i click the complete button the record should be deleted from database also...
please help
推荐答案
试试这个:
分配:
Try this:
Assign:
static int f;
static string chain;
在删除按钮中,gridview的click事件使用此代码段:
In the delete button click event of gridview use this code snippet:
using (GridViewRow row = (GridViewRow)((ImageButton)sender).Parent.Parent)
{
Label lblid1 = (Label)row.FindControl("lbl_id");
int id1 = Convert.ToInt16(lblid1.Text);
if (f == 0)
{
chain += id1.ToString();
}
else if (f != 0)
{
chain += ',' + id1.ToString();
}
f++;
ImageButton btn = (ImageButton)sender;
GridView1.Rows[row.RowIndex].Visible = false;
}
和完整按钮点击事件
and in complete button click event
string[] text = chain.Split(',');
foreach(string word in text)
{
//delete query here where id=word
//Console.WriteLine(word);
}
chain = "";
或试试:
1.从删除按钮上选定的gridview行获取ID单击
2.从Datasource(DataTable或DataSet)中删除特定记录并将选定的ID存储在变量
3.然后再将它与gridview绑定
4.on完成按钮点击获取id并删除它。
尝试使用
RemoveAt()
方法对gridview行进行软删除。
这不会从数据库中删除,只能从gridview集合中删除。
method for the gridview rows to have a soft delete.
This wont delete from the DB but only from the gridview collection.
首先需要在数据库表和列中输入列'状态' 'status'的属性将Default Value或Binding属性设置为1,因此对于每个新条目'Status'列自动设置为1.然后使用'status = 1'的行加载datagrid视图,如
First you need to and a column 'status' in your DB Table and in the Column properties of 'status' Set the Default Value or Binding property to 1 , so for every new entry 'Status' column is automatically set to 1. Then load your datagrid view with rows having 'status=1' like
select * from [Table_name] where status=1
然后在行删除网格视图事件中你应该更新你需要删除的记录状态为0
Then in the row deleting event of grid view you should update the status of record u need to delete to 0
string str = GridView1.DataKeys[e.RowIndex].Values["id"].ToString();
SqlDataAdapter da = new SqlDataAdapter("UPDATE [Table_name] set status=0 WHERE id = '" + str + "' ", connStr);
然后再次调用函数加载网格,
then again call the function to load grid,
这篇关于从网格中删除记录而不是在数据库中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!