清除按钮以清除gridview和ms访问数据库中的行 [英] Clear button to Clear the rows in gridview and ms access database

查看:58
本文介绍了清除按钮以清除gridview和ms访问数据库中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:



I have the following code:

using System.Data.OleDb;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    OleDbConnection con;
    OleDbCommand cmd;

    protected void Button1_Click(object sender, EventArgs e)
    {

        
        
            using (con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;" + @"DATA SOURCE=C:\Users\abc3\Desktop\Excel.accdb"))
            {
                cmd = new OleDbCommand();
                cmd.CommandText = "insert into ClassLevel([Class],[18th])values(@class, @18th)";

                cmd.Parameters.Add("@class", OleDbType.VarChar).Value = DropDownList1.SelectedItem.Text;
                cmd.Parameters.Add("@18th", OleDbType.VarChar).Value = TextBox2.Text;

                cmd.Connection = con;
                con.Open();
               cmd.ExecuteNonQuery();
                con.Close();
                DropDownList1.Text = "";
                TextBox2.Text = "";

            }
            BindUserDetails();
        }
        protected void BindUserDetails()
        {
        //DataSet ds = new DataSet();
            DataTable dt;
        string strquery = "SELECT * FROM ClassLevel";
        using (con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;" + @"DATA SOURCE=C:\Users\abc3\Desktop\Excel.accdb"))
        {
        using (cmd = new OleDbCommand(strquery, con))
        {
            con.Open();

        OleDbDataAdapter Da = new OleDbDataAdapter(cmd);
        dt = new DataTable();
        Da.Fill(dt);
        }
        }
        GridView1.DataSource=dt;
        GridView1.DataBind();
        }
}





下拉代码为:



Code for dropdown is:

<asp:DropDownList ID="DropDownList1" runat="server" Height="16px"  Width="113px">
 <asp:ListItem Value="0">Select Class
 <asp:ListItem Value="Class 0">Class 0
<asp:ListItem Value="Class 1">Class 1
<asp:ListItem Value="Class 2">Class 2
<asp:ListItem Value="Class 3">Class 3
<asp:ListItem Value="Class 4">Class 4





我想要添加一个清除按钮,删除gridview和ms访问数据库中的所有行。

此外,当我在下拉列表中输入0级并在与此类对应的文本框中输入新值时,应该更新值gridview以及数据库而不是添加新行。请帮助相同。



I want to add one clear button that will delete all the rows from gridview and ms access database.
Also when i enter class 0 in dropdown and enter new value in the textbox corresponding to this class then value should be updated in gridview as well as database instead of adding new row.Please help with the same.

推荐答案

写入逻辑以从数据库中删除表数据(使用截断)。

然后清除网格

Write logic to remove table data from the database (use Truncate).
Then clear the grid
Gridview1.DataSource = null;
Gridview1.DataBind();


这篇关于清除按钮以清除gridview和ms访问数据库中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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