如何在插入数据后更新Datagridview

时间:2015-01-14 00:26:19

标签: c# mysql database datagridview

我可以将我的数据插入到MySql数据库中,但是在C#应用程序上我还希望按下按钮后datagridview更新,但我不知道如何。它只在关闭C#应用程序并再次打开后才在datagridview中刷新。

private void btnEintrag_Click(object sender, EventArgs e)
{
    // Netzwerk IP-Adresse ermitteln  
    IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName());
    string IPAddress = Host.AddressList[1].ToString();

    string datasource = IPAddress;
    string database = "datenbank";
    string port = "xxx";
    string UID = "xxxx";
    string pw = "xxx";

    string myConnection;
    myConnection = string.Format("datasource=" + datasource + ";" + "Database=" + database + ";" + "port=" + port + ";" + "UID=" + UID + ";" + "password=" + pw);
    string VName, NName, Zusatztext;
    VName = txtVName.Text;
    NName = txtNName.Text;
    Zusatztext = txtZusatz.Text;

    Eintrag.CreateEntry(myConnection, VName, NName, Zusatztext); //Creates an Entry in the Table
}

private void BindGrid() //with this function I can see the data in datagridview
{
     IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName());
     string IPAddress = Host.AddressList[1].ToString();
     MessageBox.Show(IPAddress);

     string datasource = IPAddress;
     string database = "datenbank";
     string port = "3306";
     string UID = "outside";
     string pw = "mysql";

     string myConnection;
     myConnection = string.Format("datasource=" + datasource + ";" + "Database=" + database + ";" + "port=" + port + ";" + "UID=" + UID + ";" + "password=" + pw);

     using (MySqlConnection con = new MySqlConnection(myConnection))
     {
         using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Formular", con))
         {
              cmd.CommandType = CommandType.Text;

              using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
              {
                  using (DataTable dt = new DataTable())
                  {
                      sda.Fill(dt);
                      dataGridView1.DataSource = dt;
                  }
              }
          }
      }
}

private void UpdateDataGridView()
{
      ?????? I don't know 
}

0 个答案:

没有答案