C# 我想在datagrid中查看它,用户每次单击按钮时都会添加输入

C# 我想在datagrid中查看它,用户每次单击按钮时都会添加输入,c#,asp.net,sql-server,stored-procedures,C#,Asp.net,Sql Server,Stored Procedures,我想在datagrid中查看它,用户每次单击按钮时都会添加输入。我该如何使它这样做。现在它可以保存到数据库中…但我希望它在界面中以datagrid的顺序查看,用户每次单击添加数据时,它将一起保存到数据库中,并以datagrid格式显示在前端..TQ中 protected void Button2_Click(object sender, EventArgs e) { if (Session["Time"].ToString() == ViewState["Time"]

我想在datagrid中查看它,用户每次单击按钮时都会添加输入。我该如何使它这样做。现在它可以保存到数据库中…但我希望它在界面中以datagrid的顺序查看,用户每次单击添加数据时,它将一起保存到数据库中,并以datagrid格式显示在前端..TQ中

  protected void Button2_Click(object sender, EventArgs e)
    {
        if (Session["Time"].ToString() == ViewState["Time"].ToString())
        {
            SqlConnection connection = new SqlConnection("Data Source=RGUNASEL-   DESK\\SQLEXPRESS;Initial Catalog=eLogbook;User ID=sa;Password=1234");
            connection.Open();

            SqlCommand cmd = new SqlCommand("eform2", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@lot_num", SqlDbType.VarChar, 50)).Value = TextBox8.Text;
            cmd.Parameters.Add(new SqlParameter("@location", SqlDbType.VarChar, 50)).Value = TextBox9.Text;
            cmd.Parameters.Add(new SqlParameter("@total_in", SqlDbType.VarChar)).Value = TextBox10.Text;
            cmd.Parameters.Add(new SqlParameter("@first_test", SqlDbType.VarChar, 50)).Value = TextBox11.Text;
            cmd.Parameters.Add(new SqlParameter("@second_test", SqlDbType.VarChar)).Value = TextBox12.Text;
            cmd.Parameters.Add(new SqlParameter("@third_test", SqlDbType.VarChar, 50)).Value = TextBox13.Text;
            cmd.Parameters.Add(new SqlParameter("@total_out", SqlDbType.VarChar, 50)).Value = TextBox14.Text;
            cmd.Parameters.Add(new SqlParameter("@lot_status", SqlDbType.VarChar, 50)).Value = TextBox17.Text;
            cmd.Parameters.Add(new SqlParameter("@remark", SqlDbType.VarChar, 50)).Value = TextBox16.Text;

            cmd.ExecuteNonQuery();
            Response.Write("Submitted!");
            TextBox8.Text = null;
            TextBox9.Text = null;
            TextBox10.Text = null;
            TextBox11.Text = null;
            TextBox12.Text = null;
            TextBox13.Text = null;
            TextBox14.Text = null;
            TextBox17.Text = null;
            TextBox16.Text = null;


            Session["Time"] = DateTime.Now.ToString();
        }
        else
        {
            // Code for page refresh....
            TextBox8.Text = null;
            TextBox9.Text = null;
            TextBox10.Text = null;
            TextBox11.Text = null;
            TextBox12.Text = null;
            TextBox13.Text = null;
            TextBox14.Text = null;
            TextBox17.Text = null;
            TextBox16.Text = null;
            Response.Write("Page Refreshed!");
        }

    }

不需要检查时间,只需在每次添加数据/行后重新绑定gridview即可

编辑:


与其将所有单独的TextBox.Text=设置为null,为什么不使用
foreach循环并选中foreach(控件中的控件){},然后设置每个TextBox.Text=string.Empty无需对值进行硬编码如果有100个文本框会怎么样。。?当你可以写4-6行代码来处理所有100个文本框时,为什么还要写100次同样的代码呢?只是一些在原始版本之外清理代码的建议,还可以考虑将cmd.Parameters.Add()方法更改为使用cmd.Parameters.AddWithValues()我已经尝试过使用你的代码管理器..但它不起作用…我得到了这个错误“错误1当前上下文C:\Users\rgunasel\Documents\Visual Studio 2010\Projects\elogbook\u form\elogbook\u form\WebForm1.aspx.cs中不存在名称'YourFunctionToGetRequestedTableRows'。为什么会出现这种错误..请帮助'YourFunctionToGetRequestedTableRows()'这是您必须根据您的请求定义的函数名。我必须在客户端定义它,是吗?
//Code to bind gridview
Dataset dst=yourFunctionToGetRequiredTableRows();
yourGridView.DataSource=dst;
yourGridView.Databind();
protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection("Data Source=RGUNASEL-   DESK\\SQLEXPRESS;Initial Catalog=eLogbook;User ID=sa;Password=1234");
        connection.Open();
        SqlCommand cmd = new SqlCommand("eform2", connection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@lot_num", SqlDbType.VarChar, 50)).Value = TextBox8.Text;
        cmd.Parameters.Add(new SqlParameter("@location", SqlDbType.VarChar, 50)).Value = TextBox9.Text;
        cmd.Parameters.Add(new SqlParameter("@total_in", SqlDbType.VarChar)).Value = TextBox10.Text;
        cmd.Parameters.Add(new SqlParameter("@first_test", SqlDbType.VarChar, 50)).Value = TextBox11.Text;
        cmd.Parameters.Add(new SqlParameter("@second_test", SqlDbType.VarChar)).Value = TextBox12.Text;
        cmd.Parameters.Add(new SqlParameter("@third_test", SqlDbType.VarChar, 50)).Value = TextBox13.Text;
        cmd.Parameters.Add(new SqlParameter("@total_out", SqlDbType.VarChar, 50)).Value = TextBox14.Text;
        cmd.Parameters.Add(new SqlParameter("@lot_status", SqlDbType.VarChar, 50)).Value = TextBox17.Text;
        cmd.Parameters.Add(new SqlParameter("@remark", SqlDbType.VarChar, 50)).Value = TextBox16.Text;

        cmd.ExecuteNonQuery();
        //Response.Write("Submitted!");

        //Code to bind gridview
        Dataset dst=yourFunctionToGetRequiredTableRows();
        yourGridView.DataSource=dst;
        yourGridView.Databind();

    }
public Dataset yourFunctionToGetRequiredTableRows()
{
 Dataset dst=new Dataset();
 //"your SQL SELECT statement here".
 return dst;
}