2016-05-06 6 views
1

datagridviewでフル行を選択しようとしたとき、最後の列のセルの背景色が変更されません。最後のセルには、セルペインティングメソッドを使用して描画されたイメージがあります。これらの状況下でフル行を選択する方法。datagridviewセルでイメージを描画するために使用されるcellpaintingイベントの後に、フル行選択が正しく機能しない

enter image description here

private void dgvMobileOperators_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
     { 

      if (e.RowIndex >= 0 && e.ColumnIndex == 8 && dgvMobileOperators.Rows[e.RowIndex ].Cells[e.ColumnIndex+1].Value.ToString()=="1" /*&& Convert.ToInt32(e.Value.ToString()) == 1*/) 
      { 
       e.PaintBackground(e.ClipBounds, false); 
       dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString(); 
       PointF p = e.CellBounds.Location; 
       // p.X += imageList1.ImageSize.Width; 
       p.X += 24; 
       // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico"); 
       string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\connect_established.png"; 
       e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 73, 18); 
       e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p); 
       e.Handled = true; 
      } 
      else if (e.RowIndex >= 0 && e.ColumnIndex == 8 && dgvMobileOperators.Rows[e.RowIndex].Cells[e.ColumnIndex +1].Value.ToString() == "0"/*&& Convert.ToInt32(e.Value.ToString()) == 0*/) 
      { 
       e.PaintBackground(e.ClipBounds, false); 
       dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString(); 
       PointF p = e.CellBounds.Location; 
       // p.X += imageList1.ImageSize.Width; 
       p.X += 24; 
       // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico"); 
       string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\connect_no.png"; 
       e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 73, 18); 
       e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p); 
       e.Handled = true; 
      } 



     } 

答えて

0

閉じます。しかし、この:

e.PaintBackground(e.ClipBounds, false); 

ない表示の選択にそれを伝えます。

代わりに使用します。

e.PaintBackground(e.ClipBounds, true); 
0

私は問題を解決することができました。条件の画像を変更した後、現在の行が選択されているかどうかをチェックしました。最後に、選択された背景色を持つ2つの画像が追加されます

private void dgvMobileOperators_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
     { 
      if (dgvMobileOperators.SelectedRows[0].Index != e.RowIndex) 
      { 
       if (e.RowIndex >= 0 && e.ColumnIndex == 8 && dgvMobileOperators.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() == "1" /*&& Convert.ToInt32(e.Value.ToString()) == 1*/) 
       { 
        e.PaintBackground(e.ClipBounds, false); 
        dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString(); 
        PointF p = e.CellBounds.Location; 
        // p.X += imageList1.ImageSize.Width; 
        p.X += 24; 
        // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico"); 
        string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\connect_established.png"; 
        e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 79,23); 
        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p); 
        e.Handled = true; 
       } 
       else if (e.RowIndex >= 0 && e.ColumnIndex == 8 && dgvMobileOperators.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() == "0"/*&& Convert.ToInt32(e.Value.ToString()) == 0*/) 
       { 
        e.PaintBackground(e.ClipBounds, false); 
        dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString(); 
        PointF p = e.CellBounds.Location; 
        // p.X += imageList1.ImageSize.Width; 
        p.X += 24; 
        // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico"); 
        string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\connect_no.png"; 
        e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 79, 23); 
        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p); 
        e.Handled = true; 
       } 
      } 
      else 
      { 
       if (e.RowIndex >= 0 && e.ColumnIndex == 8 && dgvMobileOperators.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() == "1" /*&& Convert.ToInt32(e.Value.ToString()) == 1*/) 
       { 
        e.PaintBackground(e.ClipBounds, false); 
        dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString(); 
        PointF p = e.CellBounds.Location; 
        // p.X += imageList1.ImageSize.Width; 
        p.X += 24; 
        // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico"); 
        string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\connect-ok.png"; 
        e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 79, 23); 
        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p); 
        e.Handled = true; 
       } 
       else if (e.RowIndex >= 0 && e.ColumnIndex == 8 && dgvMobileOperators.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() == "0"/*&& Convert.ToInt32(e.Value.ToString()) == 0*/) 
       { 
        e.PaintBackground(e.ClipBounds, false); 
        dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString(); 
        PointF p = e.CellBounds.Location; 
        // p.X += imageList1.ImageSize.Width; 
        p.X += 24; 
        // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico"); 
        string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\connect-fail.png"; 
        e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 79, 23); 
        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p); 
        e.Handled = true; 
       } 
      } 



     } 
関連する問題