使用数据表或数据集在窗口申请
我需要在一个WinForms应用程序类似这样使用数据表或数据集在一个DataGridView添加一个进度条添加的gridview的进度条发现已代码等:使用数据表或数据集在窗口申请
object[] row1 = new object[] { "test1", "test2", 50 };
:
DataGridViewProgressColumn column = new DataGridViewProgressColumn();
column.HeaderText = "Status";
dataGridView1.Columns.Add(column);
,并用分配值
但我需要这个进度条在DataTable或DataSet中。
所以这是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);
}
谢谢你,从5年后回答。 – vipersassassin 2017-09-05 20:57:50
有没有这样的事情DataGridViewProgressColumn
。要获得实际的ProgressBar
列将需要一些工作 - 即嵌入在单元格内的ProgressBar
...
实现类似所需内容的最简单方法是在相关单元格中包含动画.gif文件,所以在这种情况下,您将使用DatGridViewImageColumn
并使用“PictureBox”选择所需的动画.gif作为图像。
要顺利进行,需要多线程化您希望显示进程的进程。如何做到这一点的基本大纲将是
从this amazing site选择您的动画的.gif文件。
将
Timer
导入到窗体中,并将计时器间隔设置为“50”。在你的表单上创建一个图片框,并将其大小设置为“1,1”(非常小),将图像源设置为动画.gif。
选择此
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线程中不做任何工作,因为这会导致动画与其他所有内容一起释放!
我希望这会有所帮助。
yes Likurg看到这个图片http://i.stack.imgur.com/yZHY7.png和我需要相同,但使用动态值...和datagridview的动态绑定。 – andy 2012-04-10 14:40:41