2016-05-06 1 views
1

Als ich versuchte, volle Zeile in Datagridview zu wählen, die letzte Zelle Hintergrundfarbe der Spalte ändert sich nicht. Diese letzte Zelle hat ein Bild, das unter Verwendung der Zellmalerungsmethode gezeichnet wurde. So wählen Sie die vollständige Zeile unter diesen Umständen.Vollzeilenauswahl nicht richtig arbeitet nach Ereignis cellpainting zum Zeichnen eines Bildes in einem Datagridview-Zelle verwendet

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; 
      } 



     } 

Antwort

0

Schließen. Aber dies:

e.PaintBackground(e.ClipBounds, false); 

sagt es nicht die Auswahlanzeige.

Stattdessen verwenden:

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

ich in der Lage war, das Problem zu lösen. Ich habe mit einer if-Bedingung überprüft, dass die aktuelle Zeile ausgewählt ist und nicht, nachdem ich Bilder für die Bedingungen geändert habe. Schließlich wurde 2 Bilder mit ausgewählter Hintergrundfarbe sind

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; 
       } 
      } 



     } 
Verwandte Themen