2

次のような WinForms アプリケーションで DataTable または DataSet を使用して、DataGridView にプログレス バーを追加する必要があります。

プログレスバーの列

私が見つけたすべての場所には、次のようなコードがあります。

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

そして値を割り当てます:

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

しかし、このプログレス バーが DataTable または DataSet にある必要があります。

4

2 に答える 2

5

したがって、これはmsdnの例ですが、いくつか修正が加えられています。Datagridview、Timer、Buttonを使用しています。

次に、コンピューティングにスレッドを使用する必要があります。これがお役に立てば幸いです。

 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);
    }
于 2012-04-11T06:30:52.337 に答える
0

なんてことはありませんDataGridViewProgressColumn。実際のProgressBar列を取得するには、いくつかの作業が必要です。ProgressBarつまり、セル内に埋め込まれています...

目的のようなものを実現する最も簡単な方法は、関連するセル内にアニメーション化された .gif ファイルを配置することです。この場合、 を使用しDatGridViewImageColumn、必要なアニメーション化 .gif を `PictureBox' を使用して画像として選択します。

スムーズに行うには、進行状況を表示したいプロセスをマルチスレッド化する必要があります。これを行う方法の基本的な概要は次のとおりです。

  1. このすばらしいサイトから、アニメーション化された .gif ファイルを選択してください。

  2. をフォームにインポートしTimer、タイマー間隔を「50」に設定します。

  3. フォームに画像ボックスを作成し、そのサイズを「1, 1」(非常に小さい) に設定し、画像ソースをアニメーション .gif に設定します。

  4. PictureBoxこれをDatGridViewImageColumnソース イメージとして選択します。

次に、次のコードを含めます

private void yourTimerForGifAnimation_Tick(object sender, EventArgs e)
{
    this.dataGridView1.Rows[someRow].Cells["My Progress Bar Column"].Value = 
        this.pictureBoxGif.Image            
    this.dataGridView1.InvalidateCell(cellColumnIndex, cellRowIndex);
}

これにより、アニメーション化された進捗イメージが作成されます。心配する必要があるのは、UI スレッドで作業を行わないことだけです。これにより、アニメーションが他のすべてのものと一緒に解放されます。

これが役立つことを願っています。

于 2012-04-10T14:50:39.740 に答える