DataGridViewColumn 的进度条

发布于 2024-12-02 15:22:57 字数 1748 浏览 0 评论 0 原文

此处 马克·赖德奥特,一位 Microsoft 开发人员解释了如何在 DataGridView 中创建进度栏列,并提供了“基本”代码DataGridViewProgressColumn 类。他关于如何将其用于数据绑定 datagridview 的说明如下:

如果你是数据绑定的,那么您只需更改具有 0 到 100 个值的整数列的列类型即可。

有些人,包括我自己,不明白“更改具有 0 的整数列的列类型”是什么意思通过 100 个值”,在数据绑定网格的情况下,Mark 似乎太忙而无法回答。有谁知道它是如何完成的?

这是我的案例的快速示例:

namespace Sample
{
    public class Record
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Progress { get; set; } // This is the int 0-100 field which 
                                          // has data for a progress bar

        public Record()
        {
        }

        public Record(int id, string name, int progress)
        {
            this.Id = id;
            this.Name = name;
            this.Progress= progress;
        }
    }
}

namespace Sample
{
    public partial class SampleForm : Form
    {
        private BindingList<Record> records;

        public SampleForm()
        {
            InitializeComponent();
        }

        public SampleForm(BindingList<Record> records)
        {
            InitializeComponent();
            this.records = records;
        }

        private void SampleForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = this.records;
        }
    }
}

我应该在什么时候将“进度”列类型更改为 DataGridViewProgressColumn,以及如何完成?

Here Mark Rideout, a Microsoft developer explains how to create a progress bar column in a DataGridView, and provides a "bare-bone" code for DataGridViewProgressColumn class. His instructions on how to use it for databound datagridviews are:

If you are databound, then you can just change the column type of an integer column that has 0 through 100 values.

Some people, including myself do not understand what does it mean to "change the column type of an integer column that has 0 through 100 values" in the case with databound grid, and Mark seems to be too busy to answer. Does anyone know how it is done?

Here is a quick sample of my case:

namespace Sample
{
    public class Record
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Progress { get; set; } // This is the int 0-100 field which 
                                          // has data for a progress bar

        public Record()
        {
        }

        public Record(int id, string name, int progress)
        {
            this.Id = id;
            this.Name = name;
            this.Progress= progress;
        }
    }
}

namespace Sample
{
    public partial class SampleForm : Form
    {
        private BindingList<Record> records;

        public SampleForm()
        {
            InitializeComponent();
        }

        public SampleForm(BindingList<Record> records)
        {
            InitializeComponent();
            this.records = records;
        }

        private void SampleForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = this.records;
        }
    }
}

At what point exactly should I change the "Progress" column type to DataGridViewProgressColumn, and how is that done?

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

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

发布评论

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

评论(3

被你宠の有点坏 2024-12-09 15:22:57

据我了解,您需要将此类类型的列绑定到类型为 Int32 且值可以为 0 到 100(以百分比表示)的属性。

数据输入类:

class Data
{
    public int Progress { get; set; }
}

数据绑定:

var data = new List<Data>() { 
    new Data() { Progress = 50 },
    new Data() { Progress = 60 },
    new Data() { Progress = 30 },
    new Data() { Progress = 92 },
};

this.dataGridView1.DataSource = data;

** 如下所示: **

文章中描述的类应该在向 GridView 添加列之前添加到构建的项目中。

并且不要忘记只允许从 0 到 100 的值。

步骤

在此处输入图像描述

在此处输入图像描述

As I understand you need to bind such type of columns to the properties which have type Int32 and can have values from 0 to 100 (as percentage).

Data entry class:

class Data
{
    public int Progress { get; set; }
}

Binding of the data:

var data = new List<Data>() { 
    new Data() { Progress = 50 },
    new Data() { Progress = 60 },
    new Data() { Progress = 30 },
    new Data() { Progress = 92 },
};

this.dataGridView1.DataSource = data;

** This is how it looks: **

Class, described in the article should be added to the project a built before adding columns to GridView.

And don't forget that allowed values only from 0 to 100.

Steps

enter image description here

enter image description here

enter image description here

硪扪都還晓 2024-12-09 15:22:57

我使用 VB.NET 附加链接 无论如何都是用 C# 编写的,所以你很容易理解。
包含这两个类后,只需调用表单中的命名空间,构建解决方案,然后添加一个 datagridview 控件并向该控件添加一个 DataGridViewProgressColumn 类型的列。
因此,您可以使用以下代码来填充该控件。

DataGridView1.Rows.Add(20)
DataGridView1.Rows.Add(20)

PS 我的代码示例是用 VB 编写的,但我确信您可以找出它与 C# 等效

I use VB.NET the attached Link is in C# anyway, so its going to be easy for you to Understand.
Once you include the two classes, just call the Namespace in you form, build your solution, then add a datagridview control and add a column of type DataGridViewProgressColumn to the control.
You can thus use the following code to populate the control.

DataGridView1.Rows.Add(20)
DataGridView1.Rows.Add(20)

P.S My code sample is in VB but am sure you can figure out it's equivallent for C#

还给你自由 2024-12-09 15:22:57

这对我有用,所以也许这会对某人有所帮助......

progressBar1.Bounds = dgv.GetCellDisplayRectangle(columnIndex, rowIndex, true);

This works for me, so maybe this will help someone...

progressBar1.Bounds = dgv.GetCellDisplayRectangle(columnIndex, rowIndex, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文