2017-07-27 64 views
0

我制作了一个应用程序,允许用户通过datagridview(由列和组合框组成)的方式将数据添加到数据库中。我编写了应用程序来刷新datagridview,当用户单击按钮时以及数据发送到数据库时。刷新之后,datagridview的空列将填充先前的用户填充数据。它适用于列,但不适用于我的组合框。在刷新之后,我希望组合框选择先前插入到我的数据库中的项目(组合框是用户可以选择添加到数据库的已存在项目列表)。将列数据库中的数据放入组合框中作为结果

代码我的按钮:

private void metroButton1_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[20].Value != null) && (bool)row.Cells[20].Value) 
      { 

       SqlCommand command = maConnexion.CreateCommand(); 


       command = new SqlCommand("update FailAndPass set [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]", maConnexion); 
       command.Parameters.AddWithValue("@Machine", row.Cells[1].Value != null ? row.Cells[1].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@Serial", textBox1.Text); 
       command.Parameters.AddWithValue("@pc", row.Cells[3].Value != null ? row.Cells[3].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@BName", row.Cells[4].Value != null ? row.Cells[4].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@BNumber", row.Cells[5].Value != null ? row.Cells[5].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@T", row.Cells[6].Value != null ? row.Cells[6].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@DT", row.Cells[7].Value != null ? row.Cells[7].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@TT", row.Cells[8].Value != null ? row.Cells[8].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@TS", row.Cells[9].Value != null ? row.Cells[9].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@FC", row.Cells[11].Value != null ? row.Cells[11].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@Mess", row.Cells[10].Value != null ? row.Cells[10].Value : DBNull.Value);     
       command.Parameters.AddWithValue("@TTP", row.Cells[12].Value != null ? row.Cells[12].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@RV", row.Cells[13].Value != null ? row.Cells[13].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@VR", row.Cells[14].Value != null ? row.Cells[14].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@PT", row.Cells[15].Value != null ? row.Cells[15].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@FD", row.Cells[16].Value != null ? row.Cells[16].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@RD", row.Cells[17].Value != null ? row.Cells[17].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@RT", row.Cells[18].Value != null ? row.Cells[18].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@RO", row.Cells[19].Value != null ? row.Cells[19].Value : DBNull.Value); 
       command.Parameters.AddWithValue("@FCBO", row.Cells[20].Value != null ? row.Cells[20].Value : DBNull.Value); 

       command.ExecuteNonQuery(); 




      } 
     } 

     maConnexion.Close(); 
     this.Hide(); 
     Admin admin = new Admin(); 
     admin.Show(); 
    } 

数据库: Pics 应用: pic

谢谢!

+0

检查了这一点:[GridView的回发期间丢失数据](https://stackoverflow.com/questions/4509676/gridview-loses-data-during-postback)或[GridView.DataSource回发期间为空] (https://stackoverflow.com/questions/26931784/gridview-datasource-is-null-during-postback) –

+0

@RigertaDemiri,问题是关于Winforms的DataGridView控件。 ASP.NET没有'DataGridView'控件。 – Fabio

+0

我会去看看 –

回答

0

您需要为组合框项目提供数据,并配置组合框以将选定项目与来自行的值“链接”。

var failCodes = new List<string> 
{ 
    "Fail one"; 
    "Fail two"; 
    "Fail three"; 
} 

comboboxColumn.DataSource = failCodes; 
comboboxColumn.DataPropertyName = "FailCodeByOP"; //will select item with same value 
+0

但他如何选择已经在数据库中更新的项目? –

+0

@AlexisRin,将根据您定义为“DataPropertyName”的列中的值自动选择项目。在你的情况下,当'FailCodeByOP = 1''DataGridViewComboBoxColumn'将​​尝试显示具有'Code = 1'的项目,如果在DataGridViewComboBoxColumn.DataSource中没有找到这样的项目,将会显示空单元格 – Fabio

+0

好吧,我会试试它的权利现在 –

相关问题