0

.がある Windows フォームがありDataGridViewます。そのプロパティはセル選択であり、ContextMenustripすべて選択をクリックするとすべて選択という名前のメニューがありDataGridView、選択したセルのプロパティをに変更し、選択はクリックFullRowSelectしたのと同じ行にある必要があります. 問題は、セルをクリックしたときのデフォルトのプロパティがセル選択であり、選択をクリックすると、選択しContextMenustripたセルがすべて選択されていないため、その行を再選択する必要があることですフォームが開いてクリックしたときに特定のセルで、すべてを選択ContextMenustripしてクリックすると、以前にセルをクリックしたのと同じ行が選択されます。これは私のコードです。

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
4

3 に答える 3

3

あなたの質問を正しく読んでいれば、コンテキスト メニューの [すべて選択] オプションをクリックしたときに、行全体を選択する必要があります。それが正しければ、次のことを試してみてください。

dataGridView1.SelectedCells[0].OwningRow.Selected = true;

また

foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
    cell.OwningRow.Selected = true;

最初に次の行を削除する必要があります。

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

上記の行により、DataGridView が全行選択モードになり、SelectedCells プロパティが使用されなくなり、コンテキスト メニューの [すべて選択] オプションをクリックすると、次の例外が表示されます。

Index was out of range. Must be non-negative and less than the size of the collection.

関数全体は次のようになります。

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
    foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
        cell.OwningRow.Selected = true;
}

ユーザーは、右クリックしてコンテキスト メニューを表示する前に、行を選択するセルを (左) クリックする必要があることに注意してください。それ以外の場合、選択は変更されず、以前に選択されたセルの行が選択されます。

于 2013-01-13T11:06:18.423 に答える
1

選択モードを変更する前に選択したセルを取得し、選択モードが変更された後にそれらのセルの行を選択します。

var selectedCells = dataGridView1.SelectedCells;            
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

foreach (DataGridViewCell cell in selectedCells)            
    dataGridView1.Rows[cell.RowIndex].Selected = true;

なぜあなたはそれをしなければならないのですか?DataGridView選択モードが変更されると、コントロールは選択をクリアするためです。msndのSelectionModeプロパティに関する注意事項を参照してください。

SelectionMode プロパティの値を変更すると、現在の選択がクリアされます。

于 2013-01-13T11:24:12.190 に答える
1
   private void employeesDataGridView_MouseUp(object sender, MouseEventArgs e)
    {
        DataGridView.HitTestInfo hitTestInfo;
        if (e.Button == MouseButtons.Right)
        {
            hitTestInfo = employeesDataGridView.HitTest(e.X, e.Y);
            if (hitTestInfo.Type == DataGridViewHitTestType.RowHeader || hitTestInfo.Type == DataGridViewHitTestType.Cell)
            {
                if (hitTestInfo.ColumnIndex != -1)
                    employeesDataGridView.CurrentCell = employeesDataGridView[hitTestInfo.ColumnIndex, hitTestInfo.RowIndex];
                contextMenuStrip1.Show(employeesDataGridView, employeesDataGridView.PointToClient(System.Windows.Forms.Cursor.Position));
            }
        }       
    }

    private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
    {
        employeesDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        var current = employeesDataGridView.CurrentCell;
        if (current == null) return;
        if (current.ColumnIndex == -1)
            return;
        if (current.RowIndex == -1)
            return;
        employeesDataGridView[current.ColumnIndex, current.RowIndex].Selected = true;
    }

    private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
    {
        employeesDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
    }
于 2013-01-13T11:32:36.340 に答える