繁体   English   中英

将组合框选择绑定到datagridview的当前行

[英]Bind combobox selection to currentrow of datagridview

我成功地从外部数据库填充了datagridview。 然后,我可以将组合框添加到dgv中(列[10])。 但是,当我从组合框中选择一个值时,当我尝试写回db时,它会填充dgv中的所有行(最后选择的值存储在每一行中)。 窗体中的dgv按我希望的样子显示(我可以为每行选择不同的值)。

private void bUpdate_Click(object sender, EventArgs e)
    {
        string newValue= "";
        string lineKey = "";
        int newValueLen = 0;

        SqlConnection conn = new SqlConnection(connection);

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            var newValueValue = dataGridView1.CurrentRow.Cells[10].Value;
            if (newValueValue == null)
            {
                newValueLen = 0;
            }
            else
            {
                newValueLen = dataGridView1.CurrentRow.Cells[10].Value.ToString().Length;
            }
            if (newValueLen > 0)
            {
                newValue= dataGridView1.CurrentRow.Cells[10].Value.ToString();
                lineKey = dataGridView1.CurrentRow.Cells[0].Value.ToString();

                conn.Open();
                string sqlUpdate = "Insert into tErrorLog select 'newValue = " + newValue + "; LineKey = " + lineKey + "; Cycle = " + i.ToString() + "; newValueLen = " + newValueLen.ToString() + "'";

                SqlCommand command = new SqlCommand(sqlUpdate, conn);
                command.ExecuteNonQuery();
                conn.Close();

                newValue= "";
                lineKey = "";
            }
        }
    }

当我更改至少一个值时,我在tErrorLog中获得了44个条目(这是dgv中的记录数),所有条目都具有相同的值(循环号除外)。 我确定我为newValue使用了太多变量,但是我尝试了很多排列。 一旦我开始工作,我将清理它。

提前致谢。

使用dataGridView1.Rows[i] dataGridView1.CurrentRow CurrentRow将返回当前选定的行,因此您可以保存同一行的44倍值。

建议始终使用SqlParameter创建sql查询并在其中传递值。 当您将值与查询字符串连接在一起时,您很容易受到sql注入的攻击,并且您的查询没有充分利用sql server的功能(例如,已编译的查询计划)

private void bUpdate_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(connection);

    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        var newValueValue = dataGridView1.Rows[i.Cells[10].Value;
        if (newValueValue == null)
        {
            newValueLen = 0;
        }
        else
        {
            newValueLen = dataGridView1.CurrentRow.Cells[10].Value.ToString().Length;
        }
        if (newValueLen > 0)
        {
            // Instantiate values here, so you don't need to reset them every time
            var newValue= dataGridView1.CurrentRow.Cells[10].Value.ToString();
            var lineKey = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            var logValues = 
                $"newValue = {newValue}; LineKey = {lineKey}; Cycle = {i}; newValueLen = {newValueLen}";                

            var query = "INSERT INTO tErrorLog SELECT @LogValues";
            using (var conn = new SqlConnection(connection))
            using (var command = new SqlCommand(query, conn))
            {
                var parameter = new SqlParameter
                {
                    ParameterName = "@LogValues", // Should be same as in query
                    SqlDbType = SqlDbType.VarChar, // Use actual type from your database
                    Size = 300, // Use actual size of your column in database
                    Value = logValues
                };
                command.Parameters.Add(parameter);

                conn.Open
                command.ExecuteNonQuery();
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM