首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >结果是将列数据库中的数据放入组合框

结果是将列数据库中的数据放入组合框
EN

Stack Overflow用户
提问于 2017-07-27 16:38:02
回答 1查看 350关注 0票数 0

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

我的按钮代码:

代码语言:javascript
复制
 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 Machine=@machine, ProgCode=@pc, BoardName=@BName, BoardNumber=@BNumber, Tester=@T, DateTest=@DT, TimeTest=@TT, TimeStart=@TS, FComponent=@FC, Message=@Mess, TotalTestProg=@TTP, ReadValue=@RV, ValueReference=@VR, PTolerance=@PT, FaultDetail=@FD, RepairingDate=@RD, RepairingTime=@RT, ReportingOperator=@RO, FaultCodeByOP=@FCBO  WHERE SerialNum=@Serial", 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();
    }

数据库:

应用程序:

谢谢!

EN

Stack Overflow用户

发布于 2017-07-27 16:45:00

您需要为combobox项提供数据,并将combobox配置为将选定项与行中的值“链接”。

代码语言:javascript
复制
var failCodes = new List<string>
{
    "Fail one";
    "Fail two";
    "Fail three";
}

comboboxColumn.DataSource = failCodes;
comboboxColumn.DataPropertyName = "FailCodeByOP"; //will select item with same value
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45345501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档