在 C# 中为 dataGridView 单元格提供背景图像

发布于 2024-08-11 07:27:28 字数 121 浏览 2 评论 0原文

我想使用 .net 组件 DataGridView 为行标题和列标题提供来自图像的自定义背景。甚至可以这样做吗?如果是这样,怎么办?

我使用 Visual Studio 2008、Windows 应用程序、C#。

I want to give the row headers and column headers a custom background from an image using the .net component DataGridView. Is it possible to even do this? And if so, how?

I use Visual Studio 2008, windows application, C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-08-18 07:27:28

可以更改 datagridview 行标题的属性。您需要处理 CellPainting 或 RowPostPaint 事件并手动在行标题单元格中绘制图像。

 protected override void  OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
             // Your code to insert image/content per cell goes here    
        }

Its possible to change the attributes of the datagridview rowheader. You'll need to either handle the CellPainting or the RowPostPaint event and manually draw the image in the row header cell.

 protected override void  OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
             // Your code to insert image/content per cell goes here    
        }
不知所踪 2024-08-18 07:27:28

执行此操作的方法是在 RowDataBound 事件中为每个标头元素放置一个 cssClass 名称,如下所示,并在 css 中分配背景图像。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell c in e.Row.Cells)
            {
                c.CssClass = "bgimage";
            }
        }
    }

CSS:

.bgimage{ background-image:url(images/arrow-red-large-down.gif);

On way to do this is to put a cssClass name per header element in the RowDataBound event like this and assign your background image in the css.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell c in e.Row.Cells)
            {
                c.CssClass = "bgimage";
            }
        }
    }

css:

.bgimage{ background-image:url(images/arrow-red-large-down.gif);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文