0

我想在数据网格中查看它,每次用户单击按钮时都会添加输入。我将如何做到这一点..现在它可以保存到数据库中......但我希望它在界面本身以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!");
        }

    }
4

1 回答 1

0

嘿,不需要检查时间,只需在每个数据/行添加后重新绑定网格视图。

喜欢

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

IE

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;
}
于 2013-01-11T08:05:15.770 回答