C# 从gridview中删除所有数据
我想在网页上实现一个按钮,删除gridview上显示的所有数据。有没有更简单的方法可以用按钮一次删除所有数据?您可以随时将数据源设置为nullC# 从gridview中删除所有数据,c#,asp.net,C#,Asp.net,我想在网页上实现一个按钮,删除gridview上显示的所有数据。有没有更简单的方法可以用按钮一次删除所有数据?您可以随时将数据源设置为null someGridView.DataSource = null; someGridView.DataBind(); 这很简单。只需使用遍历gridview中的每一行并获取主键值,然后使用sql查询从数据库中删除记录。 这里的代码可以帮助您。我正在使用NorthWind样本数据库 void loaddata() { SqlConne
someGridView.DataSource = null;
someGridView.DataBind();
这很简单。只需使用遍历gridview中的每一行并获取主键值,然后使用sql查询从数据库中删除记录。 这里的代码可以帮助您。我正在使用NorthWind样本数据库
void loaddata()
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabaseConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand();
connection.Open();
try
{
command = connection.CreateCommand();
command.CommandText = "SELECT * FROM Employees";
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable datatable = new DataTable();
adapter.Fill(datatable);
GridView1.DataSource = datatable;
}
catch (Exception)
{
throw;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int employee_id;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDatabaseConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand();
connection.Open();
try
{
command = connection.CreateCommand();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
employee_id = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text);
command.CommandText = "DELETE FROM Employees WHERE EmployeeID = '" + employee_id + "'";
command.ExecuteNonQuery();
}
}
catch (Exception)
{
throw;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
loaddata();
}
void loaddata()
{
SqlConnection连接=新的SqlConnection(ConfigurationManager.ConnectionString[“TestDatabaseConnectionString”].ConnectionString);
SqlCommand=newsqlcommand();
connection.Open();
尝试
{
command=connection.CreateCommand();
command.CommandText=“选择*来自员工”;
SqlDataAdapter=新SqlDataAdapter(命令);
DataTable=新的DataTable();
adapter.Fill(数据表);
GridView1.DataSource=datatable;
}
捕获(例外)
{
投掷;
}
最后
{
if(connection.State==ConnectionState.Open)
{
connection.Close();
}
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
国际雇员身份证;
SqlConnection连接=新的SqlConnection(ConfigurationManager.ConnectionString[“TestDatabaseConnectionString”].ConnectionString);
SqlCommand=newsqlcommand();
connection.Open();
尝试
{
command=connection.CreateCommand();
对于(int i=0;i
我只能像问题一样含糊不清,我仍然不太明白为什么我不能留下评论,但我可以留下答案
无论如何,我们不知道您使用什么来访问数据库或支持GridView的模型
例如,假设您有以下类支持您的GridView(GridView包含的数据类型,您已将数据源设置为):
在您的ASPX中,您将拥有以下内容:
<asp:GridView ID="GridView" runat="server"></asp:GridView>
<asp:Button ID="DeleteButton" runat="server" OnClick="DeleteButton_Click"/>
然后在你的代码背后,你会做这样的事情
protected void DeleteButton_Click(object sender, EventArgs e)
{
var gridViewItemsToDelete = (IEnumerable<MyData>)GridView.DataSource;
foreach (var idToDelete in gridViewItemsToDelete.Select(r=>r.ID))
{
// Delete the item by its ID
// I don't know what you're using to access your database
}
// Save Changes if you didn't in the foreach loop...
}
protectedvoiddeletebutton\u单击(对象发送方,事件参数e)
{
var gridViewItemsToDelete=(IEnumerable)GridView.DataSource;
foreach(gridViewItemsToDelete.Select中的var idToDelete(r=>r.ID))
{
//按项目ID删除该项目
//我不知道你用什么来访问数据库
}
//如果未在foreach循环中保存更改。。。
}
我想在单击按钮时将其删除。。。而且数据也应该从基于服务器的数据库中删除。@Som-在后端删除数据与您所问的问题完全不同……这与您发布的代码量无关,但VS2015是一个IDE
,不是框架
版本。我已经删除了Visual Studio 2015标签,因为这个问题与编码相关,而不是特定于VS。我们需要您的代码。如果您没有发布代码与之进行比较,我们如何知道是否有更简单的方法?
protected void DeleteButton_Click(object sender, EventArgs e)
{
var gridViewItemsToDelete = (IEnumerable<MyData>)GridView.DataSource;
foreach (var idToDelete in gridViewItemsToDelete.Select(r=>r.ID))
{
// Delete the item by its ID
// I don't know what you're using to access your database
}
// Save Changes if you didn't in the foreach loop...
}