2016-12-13 8 views
0

私はこのコードを使用しています:DataGridViewのCellMouseMove行の背景色の変更

private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Blue; 
} 

それが唯一の特定のセルのために動作します。行全体の色を変更したいのですが?

答えて

0

すべての行をループし、細胞とこの行の量は、それらをループし、各列にを決定するだろう

//Loop through all of the rows 
for(int i = 0; i < dataGridView1.Rows.Count; i++) { 

    //Set a variable called row for quick access to the current row 
    var row = dataGridView1.Rows[i]; 

    //Loop through all the cells in row 
    for(int x = 0; x < row.Cells.Count; x++) { 

     //Set the cell to a variable for quick access 
     var cell = row.Cells[x]; 

     //Set the back color of the cell to your desired color 
     cell.Style.BackColor = Color.Blue; 
    } 
} 

色にそれらのためのBackColorを設定し、決定することができますセルの量とループを繰り返し、その特定のセルのバックカラーを目的の色に設定します。

0

あなたがして行全体を設定することができます。これは、バックの色、それの上にマウスを移動するが、しかし、これがそうそれぞれの上にマウスのロールとして...戻ってそれを変更しないとき、それぞれの行を変更します

dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue; 

各行は色が変わり、マウスがセルを離れるときにデフォルトの白に戻りません。以下は、マウスがセルを離れるときに色を白に戻すコードです。マウスを動かしたときにセルが変更された色のままになるので、ユーザーがセルにテキストを入力するときなど、他のイベントをチェックする必要があります。

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.RowIndex >= 0) 
     dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue; 
} 

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.RowIndex >= 0) 
    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; 
}