2013-07-23 3 views
0

Ceci est ma première application Windows Form/SQL. Comment puis-je mettre à jour les modifications apportées à la base de données sur le bouton Envoyer? Lorsque j'essaie d'ajouter le datable 'dt', il ne le reconnaît pas, mais je n'arrive pas à comprendre comment placer la fonction du bouton dans l'instruction try/catch.Mettre à jour DataGridView sur le bouton Appuyez sur

De toute évidence, ils sont dans deux fonctions maintenant, mais comment puis-je faire fonctionner cette fonction en une seule fonction?

Le code est ci-dessous.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.Sql; 
using System.Data; 
using System.Data.SqlClient; 

namespace AccountsApp 
{ 
    public partial class Search : Form 
    { 
     public Search(string searchString, string searchType) 
     { 

      InitializeComponent(); 

      try 
      { 
       if (searchString != null) 
       { 
        string strCon = "Data Source=OMIW2310.orthman.local;Initial Catalog=Accts;Integrated Security=True"; 
        string strSQL = "SELECT  dbo.Account.UserName, dbo.Account.Password, dbo.Account.URL, dbo.Host.HostName, dbo.Service.ServiceName, dbo.ServiceType.SvcTypeName FROM dbo.Account INNER JOIN dbo.Host ON dbo.Account.AccountHost = dbo.Host.HostID INNER JOIN dbo.Service ON dbo.Account.AccountService = dbo.Service.ServiceID INNER JOIN dbo.ServiceType ON dbo.Account.AccountType = dbo.ServiceType.SvcTypeID WHERE " + searchType + "= '" + searchString + "'"; 
        SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon); 
        SqlCommandBuilder commandbuilder = new SqlCommandBuilder(dataAdapter); 
        DataTable dt = new DataTable(); 
        dataAdapter.Fill(dt); 
        dbGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); 
        dbGridView.DataSource = dt; 




        textBox1.Text = searchString; 
        textBox2.Text = searchType; 



       } 
      } catch (SqlException ex){ 
       MessageBox.Show(ex.Message); 
      } 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void Search_Load(object sender, EventArgs e) 
     { 

     } 

     private void textBox2_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void CancelButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      dbGridView.Update(); 
     } 

     } 
} 

Répondre

1

D'après ce que je comprends, vous voulez mettre à jour la base de données à l'aide du bouton d'envoi (que je présume être BUTTON1). Voici les modifications que j'ai apportées à votre formulaire avec des explications dans les commentaires de code:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.Sql; 
using System.Data; 
using System.Data.SqlClient; 

namespace AccountsApp 
{ 
    public partial class Search : Form 
    { 
     //make dataadapter and datatable members so they can also 
     //be used by the submit button method 
     private SqlDataAdapter dataAdapter; 
     private DataTable dt; 

     public Search(string searchString, string searchType) 
     { 

      InitializeComponent(); 

      try 
      { 
       if (searchString != null) 
       { 
        string strCon = "Data Source=OMIW2310.orthman.local;Initial Catalog=Accts;Integrated Security=True"; 
        string strSQL = "SELECT  dbo.Account.UserName, dbo.Account.Password, dbo.Account.URL, dbo.Host.HostName, dbo.Service.ServiceName, dbo.ServiceType.SvcTypeName FROM dbo.Account INNER JOIN dbo.Host ON dbo.Account.AccountHost = dbo.Host.HostID INNER JOIN dbo.Service ON dbo.Account.AccountService = dbo.Service.ServiceID INNER JOIN dbo.ServiceType ON dbo.Account.AccountType = dbo.ServiceType.SvcTypeID WHERE " + searchType + "= '" + searchString + "'"; 
        dataAdapter = new SqlDataAdapter(strSQL, strCon); 
        SqlCommandBuilder commandbuilder = new SqlCommandBuilder(dataAdapter); 
        dt = new DataTable(); 
        dataAdapter.Fill(dt); 
        dbGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); 
        dbGridView.DataSource = dt; 




        textBox1.Text = searchString; 
        textBox2.Text = searchType; 



       } 
      } catch (SqlException ex){ 
       MessageBox.Show(ex.Message); 
      } 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void Search_Load(object sender, EventArgs e) 
     { 

     } 

     private void textBox2_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void CancelButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

      //dbGridView.Update(); <------ this simply redraws the datagridview. 
      //Has nothing to do with data. 

      //code required for the update (for the submit to work) 

      //make sure the datagriview has ended editing 
      dataGridView.EndEdit(); 

      //call update on the datatable bound to your datagrdiview 
      dataAdapter.Update(dt); 
     } 
    } 
} 
+0

Cela a fonctionné! Merci beaucoup!! – CryptoJones

+0

Glad it did! :) – CristisS

Questions connexes