2017-07-13 4 views
0

データグリッドビューにコンボボックスを配置したいと思います。私は異なった方法(varリストを作成して、arraylistなどを作成する<)を試しましたが、誰も働いていません。私のデータベースがdatagridviewに表示される選択クエリのために、私の列はすでに存在しています。しかし、私のデータベースでは列がNULL値で満たされているので、列は空です。データボックスビューのコンボボックス

私は2つの選択肢があります:コンボボックスの作成をし、AddColumn関数を行う、またはあなたがする方法を知っていれば、それは、既存の列に私のコンボボックスをリンクします。そして、明らかに私はコンボボックスを作成するのに助けが必要です。

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

マイコード:

public partial class Repair : Form 
{ 


    public Repair() 
    { 
     Main pp = new Main(); 

     InitializeComponent(); 
     this.label4.Text = pp.label3.Text; 

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

     if (Program.UserType == "admin") 
     { 
      command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, RepairingTime FROM FailAndPass WHERE FComponent IS NOT NULL"; 
      command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE FComponent IS NOT NULL"; 

     } 
     else 
     { 
      command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, RepairingTime FROM FailAndPass WHERE ReportingOperator IS NULL AND FComponent IS NOT NULL"; 
      command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE ReportingOperator IS NULL AND FComponent IS NOT NULL"; 

     } 

     SqlDataAdapter sda = new SqlDataAdapter(command); 
     SqlDataAdapter sda1 = new SqlDataAdapter(command1); 
     DataTable dt = new DataTable(); 
     DataTable dt1 = new DataTable(); 
     sda.Fill(dt); 
     sda1.Fill(dt1); 

     DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool)); 
     DataColumn dcIsDirty1 = new DataColumn("IsDirty", typeof(bool)); 
     dcIsDirty.DefaultValue = false; 
     dcIsDirty1.DefaultValue = false; 




     dataGridView1.DataSource = dt; 
     dataGridView2.DataSource = dt1; 


     DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn(); 
     combo.HeaderText = "FaultCodeByOp"; 
     combo.Name = "combo"; 
     ArrayList list = new ArrayList(); 
     list.Add("C-C"); 
     list.Add("C-O"); 
     combo.Items.AddRange(list.ToArray()); 
     dataGridView1.Columns.Add(combo); 



     dt.Columns.Add(dcIsDirty); 
     dt1.Columns.Add(dcIsDirty1); 

     maConnexion.Close(); 

     dataGridView1.Columns[6].Visible = false; 
     dataGridView2.Columns[3].Visible = false; 

     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      for(int i=0;i<4;i++) 
      { 
       dataGridView1.Columns[i].ReadOnly = true; 

      } 
     } 
     foreach (DataGridViewRow row in dataGridView2.Rows) 
     { 
      for(int i=0;i<3;i++) 
      { 
       dataGridView2.Columns[i].ReadOnly = true; 
      } 
     } 




    } 




    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(); 
     SqlCommand command1 = maConnexion.CreateCommand(); 


     if (Program.UserType == "admin") 
     { 
      if (textBox1.Text != String.Empty) 
      { 

       //command.Parameters.AddWithValue("@BoardName", Var1 + "%"); 
       //command.Parameters.AddWithValue("@Machine", Var1 + "%"); 
       command.Parameters.AddWithValue("@SerialNum", Var1 + "%"); 
       command1.Parameters.AddWithValue("@SerialNum", Var1 + "%"); 
       //command.Parameters.AddWithValue("@FComponent", Var1 + "%"); 
       //command.CommandText = "SELECT * FROM FailAndPass WHERE BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent"; 

       command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE SerialNum LIKE @SerialNum AND FComponent IS NOT NULL"; 
       command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE SerialNum LIKE @SerialNum And FComponent IS NOT NULL"; 
      } 
     } 
     else 
     { 
      if (textBox1.Text != String.Empty) 
      { 

       //command.Parameters.AddWithValue("@BoardName", Var1 + "%"); 
       //command.Parameters.AddWithValue("@Machine", Var1 + "%"); 
       command.Parameters.AddWithValue("@SerialNum", Var1 + "%"); 
       command1.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 "; 
       command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE (SerialNum LIKE @SerialNum) AND ReportingOperator IS NULL AND FComponent IS NOT NULL "; 
       command1.CommandText = "SELECT DISTINCT Machine, BoardName, BoardNumber FROM FailAndPass WHERE (SerialNum LIKE @SerialNum) AND ReportingOperator IS NULL AND FComponent IS NOT NULL"; 
      } 
     } 

     if (!string.IsNullOrWhiteSpace(textBox1.Text)) 
     { 

      SqlDataAdapter sda = new SqlDataAdapter(command); 
      SqlDataAdapter sda1 = new SqlDataAdapter(command1); 
      DataTable dt = new DataTable(); 
      DataTable dt1 = new DataTable(); 
      sda.Fill(dt); 
      sda1.Fill(dt1); 

      DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool)); 
      DataColumn dcIsDirty1 = new DataColumn("IsDirty", typeof(bool)); 
      dcIsDirty.DefaultValue = false; 
      dcIsDirty1.DefaultValue = false; 
      dt.Columns.Add(dcIsDirty); 
      dt1.Columns.Add(dcIsDirty1); 

      dataGridView1.DataSource = dt; 
      dataGridView2.DataSource = dt1; 
      maConnexion.Close(); 

      dataGridView1.Columns[6].Visible = false; 
      dataGridView2.Columns[3].Visible = false; 

     } 






    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

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

     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 


      if ((row.Cells[6].Value != null) && (bool)row.Cells[6].Value) 
      { 

       SqlCommand command = maConnexion.CreateCommand(); 
       command = new SqlCommand("update FailAndPass set [email protected], [email protected], RepairingTime = @RT, [email protected] WHERE [email protected]", maConnexion); 
       command.Parameters.AddWithValue("@Fault", row.Cells[4].Value != null ? row.Cells[4].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@RD", DateTime.Today.ToString("d")); 
       command.Parameters.AddWithValue("@RT", row.Cells[5].Value != null ? row.Cells[5].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@RO", this.label4.Text); 
       command.Parameters.AddWithValue("@Serial", this.textBox1.Text); 
       command.ExecuteNonQuery(); 


      } 
     } 

     maConnexion.Close(); 
     this.Hide(); 
     Repair rep = new Repair(); 
     rep.Show(); 

    } 



    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
    { 

     if (dataGridView1.IsCurrentRowDirty) 
     { 
      dataGridView1.Rows[e.RowIndex].Cells[6].Value = true; 
     } 

    } 
} 

答えて

0
private void ComboboxInDatagridview() 
     { 
      var dataGridView1 = new DataGridView(); 
      DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn(); 
      combo.HeaderText = "FaultCodeByOp"; 
      combo.Name = "combo"; 
      dataGridView1.Columns.AddRange(new DataGridViewColumn[] { combo }); 

      ArrayList list = new ArrayList() { "C-C", "C-O" }; 

      var rowindex = dataGridView1.Rows.Add(); 
      DataGridViewComboBoxCell cmbCell = (DataGridViewComboBoxCell)dataGridView1["combo", rowindex]; 
      //Or 
      //var columnindex = 0; 
      //DataGridViewComboBoxCell cmbCell = (DataGridViewComboBoxCell)dataGridView1[columnindex, rowindex]; 
      cmbCell.DataSource = list; 
     } 
+0

こんにちは、遅く返事を申し訳ありません。私は休暇中だった。私はコードを理解しています。おそらく、「ヴァールrowIndexに= datagridview1.rows.Add()」の例外は、それは言ってあります「と、System.InvalidOperationException:。controleはデータへのリンクしているとき、プログラミングによって収集のDataGridViewに行を追加インポッシブル」 ? –

0

他のソリューションを持つ誰ですか?私は他の何かを試しました:

 DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn(); 


     ArrayList list1 = new ArrayList(); //{ "C-C", "C-O", "Absence composant", "Mauvaise valeur", "Mauvais sens", "Mauvais composant" }; 
     list1.Add("C-C"); 
     list1.Add("C-O"); 


     combo.DataSource = list1; 
     combo.HeaderText = "FaultCodeByOp"; 
     combo.DataPropertyName = "FaultCodeByOp"; 


     dataGridView1.Columns.AddRange(combo); 

didntは何でも変えます。

0

私は私が何をやったか知らないが、それはもう灰色がかったではありません。今は他の列のように白ですが、ドロップダウンやリストのメンバーを表示しません。

関連する問題