当我尝试在datagridview中选择完整行时,最后一列的单元格背景颜色不会改变。最后一个单元格具有使用单元绘制方法绘制的图像。如何在这些情况下选择Full行。
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 :(得分:0)
关闭。但是这个:
e.PaintBackground(e.ClipBounds, false);
告诉它不显示选择。
改为使用:
e.PaintBackground(e.ClipBounds, true);
答案 1 :(得分:0)
我能够解决问题。在我更改条件的图像后,我检查了if条件是否选择了当前行。最后添加了两个具有所选背景颜色的图像
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;
}
}
}