2017-07-13 257 views
0

我想在我的datagridview中放置一個組合框。我嘗試了不同的方法(創建var列表<>,創建一個數組列表等等),而且沒有任何工作。事情是我的列已經存在,因爲我的選擇查詢顯示我的數據庫在datagridview。但是列是空的,因爲在我的數據庫中列填充了NULL值。datagridview中的組合框

我有兩個選擇:創建一個組合框並做一個添加列,或者如果您知道如何操作,請將我的組合框鏈接到已存在的列。顯然,我需要幫助創建我的組合框。

謝謝!

我的代碼:有其他的解決方案

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

你好,對於遲到的回覆感到抱歉。我當時正在度假。我瞭解代碼。也許在「Var rowindex = datagridview1.rows.Add()」處有一個例外,它說「System.InvalidOperationException:當控件鏈接到數據時,通過編程將行添加到集合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); 

沒有改變任何東西。

0

我不知道我做了什麼,但它不再是灰色。現在它像其他列一樣白......但它不下拉,也不顯示列表的成員。