31

DataGridView コントロールを列を持つリストとして使用したいと思います。詳細モードの ListView のようなものですが、DataGridView の柔軟性を維持したいと考えています。

ListView (詳細ビューとFullRowSelectが有効になっている) は、行全体を強調表示し、行全体の周りにフォーカス マークを表示します。
ListView コントロールで選択された行

DataGridView ( SelectionMode = FullRowSelectを使用) は、1 つのセルの周りにのみフォーカス マークを表示します。
DataGridView で選択した行

では、DataGridView の行選択を ListView のように見せる (理想的には) 簡単な方法を知っている人はいますか?
コントロールの変更された動作を探しているのではなく、同じように見せたいだけです。
理想的には、実際の描画を行うメソッドを台無しにしないことです。

4

3 に答える 3

46

このコードをフォームのコンストラクターに入れるか、IDE を使用してdatagridview のプロパティに設定します。

dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.MultiSelect = false;
dgv.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);

次に、次のイベントをフォーム コードに貼り付けます。

private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    e.PaintParts &= ~DataGridViewPaintParts.Focus;
}

そしてそれはうまくいきます!:-)

「dgv」は問題のDataGridViewで、「form」はそれを含むFormです。

このソリューションでは、行全体に点線の長方形が表示されないことに注意してください。代わりに、フォーカス ドットを完全に削除します。

于 2008-12-01T16:56:52.077 に答える
21

どうですか

SelectionMode == FullRowSelect

ReadOnly == true

わたしにはできる。

于 2011-11-29T01:00:23.837 に答える
0

フォーカス四角形を単一のセルではなく行全体に配置する場合は、次のコードを使用できます。DataGridView の名前が gvMain で、SelectionMode が FullRowSelect に設定され、MultiSelect が False に設定されていることを前提としています。

private void gvMain_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    // Draw our own focus rectangle around the entire row
    if (gvMain.Rows[e.RowIndex].Selected && gvMain.Focused) 
        ControlPaint.DrawFocusRectangle(e.Graphics, e.RowBounds, Color.Empty, gvMain.DefaultCellStyle.SelectionBackColor);
}

private void gvMain_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    // Disable the original focus rectangle around the cell
    e.PaintParts &= ~DataGridViewPaintParts.Focus;
}

private void gvMain_LeaveAndEnter(object sender, EventArgs e)
{
    // Redraw our focus rectangle every time our DataGridView receives and looses focus (same event handler for both events)
    gvMain.InvalidateRow(gvMain.CurrentRow.Index);
}
于 2021-04-28T16:37:17.517 に答える