0

我正在开发一个应用程序,它有一个带有一列类型复选框的 Datagridview。还有一个复选框可以切换网格视图的选定行(选择/取消选择复选框)。

这是我用来实现上述逻辑的代码。

    private void gvBankUnrencolised_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        selectCells_gvBankUnreconciled();
        gvBankUnrencolised.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }

private void gvBankUnrencolised_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    selectCells_gvBankUnreconciled();
    string id = null;
    //   gvBankUnrencolised.DefaultCellStyle.BackColor = Color.White;
    int sum = 0;


    for (int i = 0; i < gvBankUnrencolised.Rows.Count; i++)
    {
        if (Convert.ToBoolean(gvBankUnrencolised.Rows[i].Cells[0].Value) == true)
        {
            //gvBankUnrencolised.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
            id = id + gvBankUnrencolised.Rows[i].Cells[1].Value + ",";
            Int32 amt = Convert.ToInt32(gvBankUnrencolised.Rows[i].Cells["AMT_WITH_SIGN"].Va

            sum = sum + amt;

        }
        else
        {
            //gvBankUnrencolised.Rows[i].DefaultCellStyle.BackColor = Color.White;
        }
    }
    if (id == null)
    {
        // MessageBox.Show("Nothing is selected");
    }
    else if (id != null)
    {
        id = id.Remove(id.Length - 1);
    }
    lblVmTotal.Text = sum.ToString();
    //lblVmTotal.Text = id;
    Bankid = id;

private void gvBankUnrencolised_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    selectCells_gvBankUnreconciled();
    string id = null;
    //   gvBankUnrencolised.DefaultCellStyle.BackColor = Color.White;
    int sum = 0;


    for (int i = 0; i < gvBankUnrencolised.Rows.Count; i++)
    {
        if (Convert.ToBoolean(gvBankUnrencolised.Rows[i].Cells[0].Value) == true)
        {
            //gvBankUnrencolised.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
            id = id + gvBankUnrencolised.Rows[i].Cells[1].Value + ",";
            Int32 amt = Convert.ToInt32(gvBankUnrencolised.Rows[i].Cells["AMT_WITH_SIGN"].Value);

            sum = sum + amt;

        }
        else
        {
            //gvBankUnrencolised.Rows[i].DefaultCellStyle.BackColor = Color.White;
        }
    }
    if (id == null)
    {
        // MessageBox.Show("Nothing is selected");
    }
    else if (id != null)
    {
        id = id.Remove(id.Length - 1);
    }
    lblVmTotal.Text = sum.ToString();
    //lblVmTotal.Text = id;
    Bankid = id;
}

private void selectCells_gvBankUnreconciled()
{
    foreach (DataGridViewRow row in gvBankUnrencolised.Rows)
    {
        if (Convert.ToBoolean(row.Cells[0].Value) == true)
            row.Selected = true;
    }
}

当数据很小(500 行)时,代码可以正常工作,但随着数据增长(>1000)行,应用程序会挂起。有没有办法做到这一点?

编辑

这是选择所有列并需要优化的代码。我不知何故留下了这条重要的信息

private void chkBankCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int j = 0; j < gvBankUnrencolised.Rows.Count; j++)
            {
                if (chkBankCheckAll.Checked == true)
                {
                    gvBankUnrencolised.Rows[j].Cells[0].Value = true;
                }
                if (chkBankCheckAll.Checked == false)
                {
                gvBankUnrencolised.Rows[j].Cells[0].Value = false;
                }
            }

        }
4

1 回答 1

1

根据我的说法,更改您的代码以执行触发内容单击的单元格的选择逻辑,而不是 DataGridView 控件中的所有行。

代码

private void gvBankUnrencolised_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    selectCells_gvBankUnreconciled(e);
    gvBankUnrencolised.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

// Some other code snippets 

private void selectCells_gvBankUnreconciled()
{
    object cellValue = gvBankUnreconciled.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
    if (celValue is DBNull) { return; }

    if (Convert.ToBoolean(cellValue) == true)
            gvBankUnreconciled.Rows[e.RowIndex].Selected = true;
}
于 2013-02-21T06:00:39.660 回答