我有一个datagridview编码在c#.net
我的要求是,如果我选择任何DataGridView单元格的单元格内容应该是可见较大的弹出,或我想查看datagridview当我的光标移动到特定的单元格时,单元格变大或合适。使选定的DataGridView单元格内容更大
0
A
回答
0
对于文本和数字,这可能做什么,你需要:
private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0 | e.ColumnIndex < 0) return;
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.CellBounds);
e.Graphics.DrawString(e.Value.ToString(), new Font(e.CellStyle.Font.FontFamily,
e.CellStyle.Font.Size * 1.5f), SystemBrushes.HighlightText, e.CellBounds.Location);
e.Handled = true;
}
}
您可能需要使用e.FormattedValue
而不是e.Value
如果你正在使用的格式
您可能还需要插入的测试单元格值的类型..
此代码会将字体放大50%,而不处于编辑模式。
对于图像不同的解决方案将是必要的 - 可能是一个弹出式标签或面板;但这真的取决于你想要什么和他们是什么样的图像。图标我会离开,用户的照片将从放大的显示中获益。
当然,如果放大的内容实际不适合在Cell弹出溶液也将被称为为..
Upadate
这里是一个测试柱的延伸Value/FormattedValue
和为Bitmap
显示Image
在弹出Label
:
Label imageLabel;
bool labelHide = false; //*** new
void showImageLabel(DataGridViewCellPaintingEventArgs e)
{
if (labelHide) return; //*** new
if (imageLabel == null) imageLabel = new Label();
imageLabel.Click += (sender, evt) =>
{ ((Label)sender).Hide(); labelHide = true; }; //*** new
imageLabel.Text = "";
imageLabel.Parent = dataGridView1;
imageLabel.Location = e.CellBounds.Location;
if (imageLabel.Image != null) imageLabel.Image.Dispose();
//Size size = ((Bitmap)e.Value).Size; //*** old
Size size = ((Bitmap)e.FormattedValue).Size; //*** new
Size newSize = new Size((int)(size.Width * 1.5f), (int)(size.Height * 1.5f));
//Bitmap bmp = new Bitmap((Bitmap)e.Value, newSize); //*** old
Bitmap bmp = new Bitmap((Bitmap)e.FormattedValue, newSize); //*** new
imageLabel.Size = newSize;
imageLabel.Image = bmp;
imageLabel.Show();
}
private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0 | e.ColumnIndex < 0) return;
if (e.Value == null) { if (imageLabel != null) imageLabel.Hide(); return; }
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
{
//if (e.Value.GetType() == typeof(Bitmap)) //*** old
if (e.FormattedValue.GetType() == typeof(Bitmap)) //*** new
{
showImageLabel(e);
e.Handled = true; //*** old
if (labelHide) labelHide = false; else e.Handled = true; //*** new
return;
}
else if (imageLabel != null) imageLabel.Hide();
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.CellBounds);
e.Graphics.DrawString(e.FormattedValue.ToString(),
new Font(e.CellStyle.Font.FontFamily, e.CellStyle.Font.Size * 1.5f),
SystemBrushes.HighlightText, e.CellBounds.Location);
e.Handled = true;
}
}
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (imageLabel != null) imageLabel.Hide();
}
您可能需要调整位置是如居中..
更新2
我现在已经适应的代码直接从数据库中检索作为Images
byte[]
的情况下。在这种情况下,Value
财产的Type
不是Image
。相反,一个simlpy需要检查FormattedValue
。
如果显示的,调整大小的Image
太大,它可能覆盖整个Cell
并且Cell_Painting
事件不会被触发。因此,我还为RowLeave
事件添加了一行,以防止发生这种情况。
我还添加了几行让图像被点击。
请更改我标记为// ***的行,添加事件并检查它是否适用于您!
这里有两个截图:
相关问题
- 1. 从DataGridView获取选定行的单元格的内容
- 2. 删除DatagridView的单元格内容
- 3. Wpf datagridview包装单元格内容
- 4. UICollectionView更新特定单元格的单元格内容
- 5. 复选框状态基于Datagridview的单元格内容列
- 6. 根据条件更改datagridview中单元格的内容
- 7. dataGridView中选定的单元格
- 8. 清除数据绑定datagridview中的单元格内容
- 9. 固定大小的HTML表格单元格,即使内容太大?
- 10. 更改单元格的内容,具体取决于单元格中的内容
- 11. 显示为datagridview的单杠为溢出单元格内容
- 12. 使用输入表单更改表格的单元格内容
- 13. 自定义datagridview单元格?
- 14. DataGridView单元格自定义
- 15. 如何重复选定的单元格或单元格的内容?
- 16. UItableView单元格内容大小为iPad
- 17. 如何指定大虾截断表格单元格内容?
- 18. DataGridView单元格
- 19. 如果DataGridView的单元格值大于另一个单元格
- 20. 单元格值更改时更新dataGridView
- 21. 访问单元格datagridview的内容Winform C#
- 22. 基于Datagridview C#中的内容着色组单元格#
- 23. 还原DataGridView的单元格内容原值
- 24. 单元格内容删除后保存DataGridView的最佳做法
- 25. 表格单元格内容
- 26. 单元格内容(Textview)只触摸单元格后更新
- 27. 当前单元格被更改时清除单元格内容
- 28. 如何使固定大小的表格单元格与其内容无关
- 29. winforms gridview单元格内容已更改
- 30. Datagridview更改非空单元格的单元格颜色
会不会有在细胞哪些类型的内容?只是文本和数字或图像,按钮等?另外:放大后内容是否仍可编辑? – TaW 2014-09-11 10:52:21
主要内容是文字和图片,有些是数字......最后所有类型的表演都在那里@TaW – 2014-09-11 10:58:41
你解决了你的问题吗? – TaW 2014-09-18 07:26:39