c# - 在窗口应用程序中使用数据表或数据集在gridview中添加进度条

标签 c# winforms datatable dataset progress-bar

我需要在 WinForms 应用程序中使用 DataTable 或 DataSet 在 DataGridView 中添加进度条,与此类似:

progress bar column

我发现的每个地方都有这样的代码:

DataGridViewProgressColumn column = new DataGridViewProgressColumn();
column.HeaderText = "Status";
dataGridView1.Columns.Add(column);

并赋值:

object[] row1 = new object[]  { "test1", "test2", 50 };

但我需要这个进度条位于 DataTable 或 DataSet 中。

最佳答案

这是来自 msdn 的示例,经过一些修正。我使用 Datagridview、计时器、按钮。

现在您需要使用线程进行计算。我希望这会有所帮助。

 public class DataGridViewProgressColumn : DataGridViewImageColumn
    {
        public DataGridViewProgressColumn()
        {
            CellTemplate = new DataGridViewProgressCell();
        }
    }

    class DataGridViewProgressCell : DataGridViewImageCell
    {
        // Used to make custom cell consistent with a DataGridViewImageCell
        static Image emptyImage;
        static DataGridViewProgressCell()
        {
            emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        }
        public DataGridViewProgressCell()
        {
            this.ValueType = typeof(int);
        }
        // Method required to make the Progress Cell consistent with the default Image Cell. 
        // The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an int.
        protected override object GetFormattedValue(object value,
                            int rowIndex, ref DataGridViewCellStyle cellStyle,
                            TypeConverter valueTypeConverter,
                            TypeConverter formattedValueTypeConverter,
                            DataGridViewDataErrorContexts context)
        {
            return emptyImage;
        }

        protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            int progressVal = 0;

            if (value != null)
                progressVal = (int)value;

            float percentage = ((float)progressVal / 100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%.
            Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
            Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
            // Draws the cell grid
            base.Paint(g, clipBounds, cellBounds,
             rowIndex, cellState, value, formattedValue, errorText,
             cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
            if (percentage > 0.0)
            {
                // Draw the progress bar and the text
                g.FillRectangle(new SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
                g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
            }
            else
            {
                // draw the text
                if (this.DataGridView.CurrentRow.Index == rowIndex)
                    g.DrawString(progressVal.ToString() + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2);
                else
                    g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DataGridViewProgressColumn column = new DataGridViewProgressColumn();
        dataGridView1.Columns.Add(new DataGridViewProgressColumn());
        dataGridView1.Rows.Add("Row 1", 10);
        dataGridView1.Rows.Add("Row 2", 50);
        dataGridView1[1, 0].Value = 10;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Random r=new Random();
        dataGridView1[1, 0].Value = r.Next(0, 100);
        dataGridView1[1, 1].Value = r.Next(0, 100);
    }

关于c# - 在窗口应用程序中使用数据表或数据集在gridview中添加进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10090691/

相关文章:

c# - 如何处理 XmlReader.Create 的异常

c# - 一种形式关闭 - 第二次更新 DataGridView

c# - 指定的转换无效。(数据表中的整数值)

angular - 如何去除 Angular 材表的边框?

r - 在 R 数据表中查找字符串的第一次迭代

c# - Awesomium.NET 在调整大小时崩溃 - 内存泄漏

c# - 未实现 INPC 的对象如何通知更改?

visual-studio - .NET VS2005 WinForms : How do i drop a user control onto a form?

c# - 从 xml 加载信息,使用 c# 保存相关(更改)信息

c# - 在通过 EF6 上的 Code First 实现创建的 MySql 表上创建 PARTITION