更新数据库中DataGridView CheckBox的值。 [英] Update value of DataGridView CheckBox in database.

查看:104
本文介绍了更新数据库中DataGridView CheckBox的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在构建一个Windows应用程序,我想更新数据库中datagridview的已检查行。我已经尝试了很多选项但是没有得到我想要的结果。以下是我尝试的代码:



Hi,

I m building a windows application and I want to update the checked rows of the datagridview in the database. I have tried many options but m not getting the result as i want. Following is my code that i m trying :

foreach (DataGridViewRow dr in dgvPendingApprovals.Rows)
{
    int rid = int.Parse(dgvPendingApprovals[1, dr.Index].Value.ToString());

    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
    ch1 = (DataGridViewCheckBoxCell)dgvPendingApprovals.Rows[dgvPendingApprovals.CurrentRow.Index].Cells[0];
    if (ch1.Value == null)
        ch1.Value = false;
    switch (ch1.Value.ToString())
    {
        case "True":
            ch1.Value = false;
            break;
        case "False":
            cmd.CommandText = "Update ApplicantDetails set Status2 = 'Yes', ApprovedDate2 =  getdate() where Id = '" + rid + "'";
            cmd.Connection = conobj.getConn();
            conobj.openCon();
            cmd.ExecuteNonQuery();
            conobj.closeCon();
            rid = 0;
            MessageBox.Show(rid + " Updated");
            break;
    }

}






我希望每个数据行对于checkbox.check值为true的datarow,该行应该更新。但是在使用此代码时,只会更新所选行,而不会选中其复选框。请帮我解决此问题。



I want that for each datarow that for datarow whose checkbox.check value is true, that row should get updated. But when using this code only the row that is selected gets updated, not the one whose checkbox is checked. Please help me with a solution for this.

推荐答案

从更新查询中删除WHERE子句
Remove WHERE clause from your Update Query


我得到了解决方案.. .....它按照我想要的方式工作......以下是代码。



I got the solution....... and it is working the way i want... following is the code.

foreach (DataGridViewRow dr in dgvPendingApprovals.Rows)
{
    int rid = int.Parse(dgvPendingApprovals[1, dr.Index].Value.ToString());
    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
    ch1 = (DataGridViewCheckBoxCell)dr.Cells[0];
    try
    {
        switch (ch1.Value.ToString())
        {
            case "False":
                ch1.Value = false;
                break;
            case "True":
                cmd.CommandText = "Update ApplicantDetails set Status2 = 'Yes', ApprovedDate2 =  getdate() where Id = '" + rid + "'";
                cmd.Connection = conobj.getConn();
                conobj.openCon();
                cmd.ExecuteNonQuery();
                conobj.closeCon();
                rid = 0;
                break;
        }
    }
    catch { }
}


这篇关于更新数据库中DataGridView CheckBox的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆