以下代码是在当前时间和事务创建之间经过一定时间后应该处理事务(当然存储在MS-SQL数据库中)的程序的一部分。为了视觉效果,我有一个进度条跟踪每个事务的经过时间。然而,即使我有不同的创建时间,理论上应该产生交错的进度条,但它们最终会同步。任何建议将不胜感激。谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Shelbyville
{
public partial class Registry : Form
{
System.Timers.Timer tTimer;
SqlConnection conn;
public Registry()
{
InitializeComponent();
}
private void Registry_Load(object sender, EventArgs e)
{
#region
string connString = "A connection string that's not the problem";
conn = new SqlConnection(connString);
string sql = @"
SELECT
Time
FROM TestTable
";
try
{
using (SqlCommand cmd = new SqlCommand())
using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
using (DataSet ds = new DataSet())
{
conn.Open();
da.Fill(ds, "Test");
DataTable dt = ds.Tables["Test"];
dgvShelbyville.DataSource = dt;
DataGridViewProgressColumn progCol = new DataGridViewProgressColumn();
dgvShelbyville.Columns.Add(progCol);
progCol.HeaderText = "Progress";
progCol.Name = "Progress";
dgvShelbyville.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvShelbyville.AllowUserToAddRows = false;
conn.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
conn.Close();
}
finally
{
conn.Close();
}
#endregion
tTimer = new System.Timers.Timer();
tTimer.Interval = 1000;
tTimer.Elapsed += OnTimeEvent;
tTimer.Start();
}
private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
{
Invoke(new Action(() =>
{
int columnIndex = dgvShelbyville.CurrentCell.ColumnIndex;
int rowIndex = dgvShelbyville.CurrentCell.RowIndex;
DataGridViewRow selectedRow = dgvShelbyville.Rows[rowIndex];
for (int j = 0; j < dgvShelbyville.Rows.Count; j++)
{
DateTime sTime = DateTime.Parse(selectedRow.Cells["Time"].Value.ToString());
dgvShelbyville.Rows[j].Cells["Progress"].Value = Convert.ToInt32((DateTime.Now - sTime).TotalSeconds).ToString();
}
}));
//throw new NotImplementedException();
}
#region Customer Progress Bar
public class DataGridViewProgressColumn : DataGridViewImageColumn
{
public DataGridViewProgressColumn()
{
CellTemplate = new DataGridViewProgressCell();
}
}
class DataGridViewProgressCell : DataGridViewImageCell
{
static Image emptyImage;
static DataGridViewProgressCell()
{
emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
public DataGridViewProgressCell()
{
this.ValueType = typeof(int);
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
return emptyImage;
//return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
try
{
int progressVal = 0;
if (value != null)
{
//progressVal = (int)value;
progressVal = Convert.ToInt32(value);
}
if (progressVal > 100)
{
progressVal = 100;
}
float percentage = ((float)progressVal / 100.0f);
Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (percentage > 0.0)
{
graphics.FillRectangle(new SolidBrush(Color.FromArgb(203, 235, 108)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32(percentage * cellBounds.Width - 4), cellBounds.Height - 4);
graphics.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + (cellBounds.Width / 2) - 5, cellBounds.Y + 2);
}
else
{
if (this.DataGridView.CurrentRow.Index == rowIndex)
graphics.DrawString(progressVal.ToString() + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2);
else
graphics.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
}
}
}
#endregion