填充datagridview时更新数据库并单击按钮

时间:2017-07-11 11:37:21

标签: c# sql sql-server visual-studio datagridview

经过几个小时的研究,我仍然不知道我应该如何编码。我想在用户填写datagridview的某些特定列时更新我的​​数据库,然后点击按钮。

我看到了一些绑定,一些datatable和一些dataset。我从来没有听说过databinding,也无法更改我的所有代码以尊重databinding关闭。我已经看到了datatable的一些内容,但它与dataset相同吗? (数据集只是一个集合?)。我目前正在使用dataset,但我测试的所有内容都无法使用。

这是我在实际测试中的代码:

public partial class Repair : Form
{
    public Repair()
    {
        InitializeComponent();

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

        if (Program.UserType == "admin")
        {
            command.CommandText = "SELECT * FROM FailOnly";
        }
        else
        {
            command.CommandText = "SELECT * FROM FailOnly WHERE ReportingOperator IS NULL";
        }

        SqlDataAdapter sda = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        maConnexion.Close();
    }

    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();

        if (Program.UserType == "admin")
        {
            if (textBox1.Text != "")
            {
                command.Parameters.AddWithValue("@BoardName", Var1 + "%");
                command.Parameters.AddWithValue("@Machine", Var1 + "%");
                command.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";
            }
        }
        else
        {
            if (textBox1.Text != "")
            {
                command.Parameters.AddWithValue("@BoardName", Var1 + "%");
                command.Parameters.AddWithValue("@Machine", Var1 + "%");
                command.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  ";
            }
        }

        SqlDataAdapter sda = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        maConnexion.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        int var;

        var = dataGridView1.Rows.Count;

        for (i = 0; i < var; i++)
        {
            SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
            maConnexion.Open();
            SqlCommand command = maConnexion.CreateCommand();
            command = new SqlCommand("update FailOnly, FailAndPass set FaultCodeByOp=@Fault, RepairingDate=@RD, RepairingTime = @RT, ReportingOperator=@RO", maConnexion);
            command.Parameters.AddWithValue("@Fault", dataGridView1.Columns[15]);
            command.Parameters.AddWithValue("@RD", dataGridView1.Columns[16]);
            command.Parameters.AddWithValue("@RT", dataGridView1.Columns[17]);
            command.Parameters.AddWithValue("@RO", dataGridView1.Columns[18]);

            command.ExecuteNonQuery();
            maConnexion.Close();
        }
    }
}

谢谢!

编辑:感谢克里希纳!非常感谢他和我一起在我的代码上过了一小时!

1 个答案:

答案 0 :(得分:0)

您正在遍历行并检查列,更改按钮单击事件,如下所示。

更改下面的数据绑定

public Repair()
{
    InitializeComponent();

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

    if (Program.UserType == "admin")
    {
        command.CommandText = "SELECT * FROM FailOnly";
    }
    else
    {
        command.CommandText = "SELECT * FROM FailOnly WHERE ReportingOperator IS NULL";
    }

    SqlDataAdapter sda = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool));
    dcIsDirty.DefaultValue = false;
    dt.Columns.Add(dcIsDirty);
    dataGridView1.DataSource = dt;
    maConnexion.Close();
    dataGridView1.Columns[19].DefaultCellStyle.NullValue = false;
}

实施CellEndEdit

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.Rows[e.RowIndex].Cells[dataGridView1.ColumnCount - 1].Value = true;
    }
}

现在按钮点击更改

private void button1_Click(object sender, EventArgs e)
{
    foreach(DataGridViewRow row in dataGridView1.Rows)
    {
        if(row.Cells[19].Value !== null && (bool)row.Cells[19].Value)//update only changed data
        {
            SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
            maConnexion.Open();
            SqlCommand command = maConnexion.CreateCommand();
            command = new SqlCommand("update FailOnly, FailAndPass set FaultCodeByOp=@Fault, RepairingDate=@RD, RepairingTime = @RT, ReportingOperator=@RO", maConnexion);
            command.Parameters.AddWithValue("@Fault",row.Cells[15].Value != null ? row.Cells[15].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RD", row.Cells[16].Value != null ? row.Cells[16].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RT", row.Cells[17].Value != null ? row.Cells[17].Value : DBNull.Value);
            command.Parameters.AddWithValue("@RO", row.Cells[18].Value != null ? row.Cells[18].Value : DBNull.Value);
            command.ExecuteNonQuery();
            maConnexion.Close();
        }
    }
}