更新DataGridView选定的行 [英] Updating DataGridView Selected Rows

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

问题描述

我试图更新DataGridView中的选定行,但是结果很奇怪,它总是缺少一行或另一行。问题是,当我单击btnSettled按钮设置结算日期,然后单击btnUpdate来更新数据库时,结果似乎还可以,但是单击btnRefresh刷新DGV之后,总是缺少一行。是UpdateCommand或foreach循环上的问题吗?请帮我解决这个问题。谢谢。

I tried to update selected rows in DataGridView, but the result is strange, it always missing a row or another. The problem is when I click btnSettled button to set the settled date, then click btnUpdate to update the database, the result seems ok, but after click btnRefresh to refresh the DGV, there is always a missing row. Is that the problem on UpdateCommand or foreach loop? Please help me to solve this problem. Thank you.

在单击btnSettle

before click btnSettle

点击btnSetted和btnUpdate之后,

after click btnSettled and btnUpdate

点击btnRefresh

after click btnRefresh

我的代码如下:

DataTable dtTrx = new DataTable();
SqlDataAdapter daTrx = new SqlDataAdapter();
DataSet dsTrx = new DataSet();

    public Form1()
    {
        InitializeComponent();
        getData();
    }

    private void getData()
    {
        string strConn = "Data Source=.\\xpw;Initial Catalog=MyStock;Integrated Security=True;";
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();

        string sqlTrx = "SELECT TrxID, TrxDate,Ticker,Qty,Price,Type,AccID, SettledDate,BrokerUserID FROM Trx";

        daTrx = new SqlDataAdapter(sqlTrx, conn);
        SqlCommandBuilder cbTrx = new SqlCommandBuilder(daTrx);
        daTrx.Fill(dsTrx, "trx");

        conn.Close();

        dtTrx = dsTrx.Tables["trx"];
        dgvTrx.DataSource = dtTrx;
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        daTrx.Update(dsTrx, "trx");
    }

    private void btnRefresh_Click(object sender, EventArgs e)
    {
        dsTrx.Clear();
        daTrx.Fill(dsTrx, "trx");
    }

    private void btnSettled_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewCell c in dgvTrx.SelectedCells)
        {
            dgvTrx[7, c.RowIndex].Value = "2017/7/23";
        }
    }


推荐答案

第一个您需要使用的所有参数都开始使用参数化SQL查询。

First of all you need start using parameterized SQL queries.

第二,我看不到您的代码有问题,但是您可以尝试这样做:

Secondly I don't see a problem with your code, but you try this :

private void btnSettled_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow r in dgvTrx.SelectedRows)
    {
        r.Cells["SettledDate"].Value = "2017/7/23"; //use the column name instead of column index
    }
    this.BindingContext[dgvTrx.DataSource].EndCurrentEdit(); 
    //the above line is added to improve the solution
    //as per the link mentioned in the accepted answer
}

这种方法背后的原因是,现在即使您更改列的位置,也不必重新编写代码以匹配更改

The reason behind this approach is that now even if you change the column position, you won't have to re-write the code to match the changes

当您使用 SelectedCells 时,因此,除非将鼠标拖到最后一个 Cell 不会添加到 SelectedCell 集合

As you are using SelectedCells, thus unless your mouse is dragged over to the last Cell it won't be added in the SelectedCell collection

注意:在中r.Cells [ SettledDate]。Value 我假定列名称为 SettledDate

Note: in r.Cells["SettledDate"].Value I assumed the column name is SettledDate

这篇关于更新DataGridView选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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