0

タイプチェックボックスの1列を持つDatagridviewを持つアプリケーションに取り組んでいます。もう 1 つのチェック ボックスがあり、グリッド ビューの選択された行を切り替えます (チェック ボックスの選択/選択解除)。

上記のロジックを実装するために使用しているコードを次に示します。

    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 に答える