2017-07-11 1 views
0

数時間の研究の後、私はこれをどのようにコード化すべきかについてはまだ分かりません。ユーザーがdatagridviewの特定の列を記入し、ボタンをクリックすると、データベースを更新したいと考えています。DataGridviewが塗りつぶされているときにデータベースを更新し、ボタンをクリック

いくつかのバインディングがありました。一部はdatatableで、一部はdatasetです。私はdatabindingについて聞いたことがなく、databindingクロージャを尊重するために私のすべてのコードを変更することはできません。私はdatatableについていくつか見てきましたが、それはdatasetと同じですか? (データセットは単にコレクションですか?)。私は現在datasetを使用していますが、テストしているすべてが機能していません。ここで

は、実際の試験では私のコードです:

public partial class Repair : Form 
{ 
    public Repair() 
    { 
     InitializeComponent(); 

     SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; "); 
     maConnexion.Open(); 
     SqlCommand command = maConnexion.CreateCommand(); 

     if (Program.UserType == "admin") 
     { 
      command.CommandText = "SELECT * FROM FailOnly"; 
     } 
     else 
     { 
      command.CommandText = "SELECT * FROM FailOnly WHERE ReportingOperator IS NULL"; 
     } 

     SqlDataAdapter sda = new SqlDataAdapter(command); 
     DataSet ds = new DataSet(); 
     sda.Fill(ds); 
     dataGridView1.DataSource = ds.Tables[0]; 
     maConnexion.Close(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 

     Main ff = new Main(); 
     ff.Show(); 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; "); 
     maConnexion.Open(); 
     string Var1 = textBox1.Text; 
     SqlCommand command = maConnexion.CreateCommand(); 

     if (Program.UserType == "admin") 
     { 
      if (textBox1.Text != "") 
      { 
       command.Parameters.AddWithValue("@BoardName", Var1 + "%"); 
       command.Parameters.AddWithValue("@Machine", Var1 + "%"); 
       command.Parameters.AddWithValue("@SerialNum", Var1 + "%"); 
       command.Parameters.AddWithValue("@FComponent", Var1 + "%"); 
       command.CommandText = "SELECT * FROM FailOnly WHERE BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent"; 
      } 
     } 
     else 
     { 
      if (textBox1.Text != "") 
      { 
       command.Parameters.AddWithValue("@BoardName", Var1 + "%"); 
       command.Parameters.AddWithValue("@Machine", Var1 + "%"); 
       command.Parameters.AddWithValue("@SerialNum", Var1 + "%"); 
       command.Parameters.AddWithValue("@FComponent", Var1 + "%"); 
       command.CommandText = "SELECT * FROM FailOnly WHERE (BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent) AND ReportingOperator IS NULL "; 
      } 
     } 

     SqlDataAdapter sda = new SqlDataAdapter(command); 
     DataSet ds = new DataSet(); 
     sda.Fill(ds); 
     dataGridView1.DataSource = ds.Tables[0]; 
     maConnexion.Close(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     int i = 0; 
     int var; 

     var = dataGridView1.Rows.Count; 

     for (i = 0; i < var; i++) 
     { 
      SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; "); 
      maConnexion.Open(); 
      SqlCommand command = maConnexion.CreateCommand(); 
      command = new SqlCommand("update FailOnly, FailAndPass set [email protected], [email protected], RepairingTime = @RT, [email protected]", maConnexion); 
      command.Parameters.AddWithValue("@Fault", dataGridView1.Columns[15]); 
      command.Parameters.AddWithValue("@RD", dataGridView1.Columns[16]); 
      command.Parameters.AddWithValue("@RT", dataGridView1.Columns[17]); 
      command.Parameters.AddWithValue("@RO", dataGridView1.Columns[18]); 

      command.ExecuteNonQuery(); 
      maConnexion.Close(); 
     } 
    } 
} 

ありがとうございました!

編集:クリシュナに感謝しています!私のコードで時間を過ごすために彼に感謝します!

+0

を変更しない

変更が動作しませんか?何かエラーが出ますか? – Krishna

+0

System.ArgumentExceptionがあります: 'オブジェクト型System.Windows.Forms.DataGridViewTextBoxColumnおよび別の管理オブジェクトへのリンクはありません':/ –

+0

更新ステートメントは2つのテーブルを使用していますか?この声明は何を意味していますか? 'Update FailOnly、FailAndPass' – Krishna

答えて

0

行をループして列をチェックしているときは、次のようにボタンのクリックイベントを変更します。

public Repair() 
{ 
    InitializeComponent(); 

    SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; "); 
    maConnexion.Open(); 
    SqlCommand command = maConnexion.CreateCommand(); 

    if (Program.UserType == "admin") 
    { 
     command.CommandText = "SELECT * FROM FailOnly"; 
    } 
    else 
    { 
     command.CommandText = "SELECT * FROM FailOnly WHERE ReportingOperator IS NULL"; 
    } 

    SqlDataAdapter sda = new SqlDataAdapter(command); 
    DataTable dt = new DataTable(); 
    sda.Fill(dt); 
    DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool)); 
    dcIsDirty.DefaultValue = false; 
    dt.Columns.Add(dcIsDirty); 
    dataGridView1.DataSource = dt; 
    maConnexion.Close(); 
    dataGridView1.Columns[19].DefaultCellStyle.NullValue = false; 
} 

CellEndEdit

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    if (dataGridView1.IsCurrentCellDirty) 
    { 
     dataGridView1.Rows[e.RowIndex].Cells[dataGridView1.ColumnCount - 1].Value = true; 
    } 
} 

を実装し、以下のようなデータバインディングは、現在ボタンのクリックを使用すると、ループの中で書いたものは何でも

private void button1_Click(object sender, EventArgs e) 
{ 
    foreach(DataGridViewRow row in dataGridView1.Rows) 
    { 
     if(row.Cells[19].Value !== null && (bool)row.Cells[19].Value)//update only changed data 
     { 
      SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; "); 
      maConnexion.Open(); 
      SqlCommand command = maConnexion.CreateCommand(); 
      command = new SqlCommand("update FailOnly, FailAndPass set [email protected], [email protected], RepairingTime = @RT, [email protected]", maConnexion); 
      command.Parameters.AddWithValue("@Fault",row.Cells[15].Value != null ? row.Cells[15].Value : DBNull.Value); 
      command.Parameters.AddWithValue("@RD", row.Cells[16].Value != null ? row.Cells[16].Value : DBNull.Value); 
      command.Parameters.AddWithValue("@RT", row.Cells[17].Value != null ? row.Cells[17].Value : DBNull.Value); 
      command.Parameters.AddWithValue("@RO", row.Cells[18].Value != null ? row.Cells[18].Value : DBNull.Value); 
      command.ExecuteNonQuery(); 
      maConnexion.Close(); 
     } 
    } 
} 
+0

私はいくつかの誤りがあります。まず、セルは行に存在しません。そして、第二に、私はまだ "executeNonQuery()"の例外があります:/ –

+0

あなたはその質問が間違っていると伝えました。 – Krishna

+0

Datagridviewの行に再びセルチェックがあります – Krishna

関連する問題