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

时间:2013-01-11 07:53:36

标签: c# asp.net sql-server stored-procedures

我想在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!");
        }

    }

1 个答案:

答案 0 :(得分:0)

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

//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;
}