添加新记录后在datagrid中显示数据

时间:2017-06-13 03:57:05

标签: c# database datagridview

我希望在保存新记录后在dataGridView中显示数据库中的数据。单击按钮后,数据已保存,但未在datagridview中显示。如何显示该数据?

private void btn_add_Click(object sender, EventArgs e)
    {

        {

            if (textBox_tarikh.Text == "" || textBox_resit.Text == "" || textBox_bayaran.Text == "")
            {
                MessageBox.Show("Please Fill In The Blank");

            }
            else
            {

                    String bResult = textBox_ic.Text;
                    string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\acap\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30"; // add your conncetion string here
                    SqlConnection connection = new SqlConnection(connectionString);
                    SqlCommand cmd = new SqlCommand("INSERT Pembayaran (Description, Date, No_Resit, Payment, Studentic) VALUES (@Description, @date, @resit, @payment, @val)", connection);
                    cmd.Parameters.AddWithValue("@val", bResult);
                    cmd.Parameters.AddWithValue("@Description", label4.Text);
                    cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(textBox_tarikh.Text));
                    cmd.Parameters.AddWithValue("@resit", textBox_resit.Text);
                    cmd.Parameters.AddWithValue("@payment", textBox_bayaran.Text);
                    SqlDataAdapter dataadapter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    connection.Open();
                    dataadapter.Fill(ds, "pembayaran_table");
                    connection.Close();
                    dataGridView3.DataSource = ds;
                    dataGridView3.DataMember = "pembayaran_table";

                   cmd.Connection.Open();
                    try
                    {
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Data saved Successfully");
                }
                    catch (Exception ex)
                    {
                        //throw new Exception("Error " + ex.Message);
                        MessageBox.Show("Receipt No. is already use");
                    }

            }

3 个答案:

答案 0 :(得分:0)

首先,我在您的代码段中看不到任何数据绑定代码,例如DataGridView.DataSource = " "DataGridView.DataBind();

插入数据后可以尝试执行此操作,使用@@IDENTITY检索已插入内容的ID。然后使用检索到的id来捕获新记录。

@@IDENTITY

我注意到在您的代码段中,您使用了相同的查询。

SqlDataAdapter dataadapter = new SqlDataAdapter(cmd);

您应该写另一个SqlStatement并从SqlDataAdapter

调用它

例如:

// Assumes that connection is a valid SqlConnection object.  
string queryString =   
  "SELECT CustomerID, CompanyName FROM dbo.Customers";  
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);  

DataSet customers = new DataSet();  
adapter.Fill(customers, "Customers");  

Populating a DataSet from a DataAdapter

答案 1 :(得分:0)

您只需在将记录插入数据库后更新DataGridView的数据源。

答案 2 :(得分:0)

您可以尝试:

private void btn_add_Click(object sender, EventArgs e)
{
   string bResult = textBox_ic.Text;
   if (textBox_tarikh.Text == "" || textBox_resit.Text == ""||textBox_bayaran.Text == "")
        {
            MessageBox.Show("Please Fill In The Blank");
        }
   string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\acap\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30";
   SqlConnection connection = new SqlConnection(connectionString);
   SqlCommand cmd ;
   try
    {
      if(connection.State == ConnectionState.Closed) connection.Open();
      cmd = new SqlCommand("INSERT Pembayaran (Description, Date, No_Resit, Payment, Studentic) VALUES (@Description, @date, @resit, @payment, @val)", connection);
      cmd.Parameters.AddWithValue("@val", bResult);
      cmd.Parameters.AddWithValue("@Description", label4.Text);
      cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(textBox_tarikh.Text));
      cmd.Parameters.AddWithValue("@resit", textBox_resit.Text);
      cmd.Parameters.AddWithValue("@payment", textBox_bayaran.Text);
      cmd.Executenonquery();

      SqlDataAdapter da = new SqlDataAdapter("Select * from Pembayaran",connection);
      DataTable dt = new DataTable();
      da.Fill(dt);
      dataGridView3.DataSource = dt;
    }
   catch(Exception ex)
   {
   }
   finally
   {
      if(connection.State == ConnectionState.Open) connection.Close();
   }
}