如何在datagridview c#中的指定时间内以指定的时间跨度启动秒表

时间:2013-11-13 06:54:32

标签: c# datagridview

我有使用System.Diagnostics在数据网格视图中运行计时器的代码,但是时间从00:00:00开始,我希望指定的时间启动计时器。可能吗??怎么样?? 请帮助.....我刚刚在c#中新手,我真的很困惑。请帮忙......

感谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TryRoadrunner
{
public partial class Form1 : Form
{
    System.Windows.Forms.Timer timer1 = new Timer();

    DataTable dtStopwatches = new DataTable();
    Dictionary<int, Stopwatch> swDct = new Dictionary<int, Stopwatch>();
    Dictionary<int, Stopwatch> wDct = new Dictionary<int, Stopwatch>();


    int nextID = 0;
    int nextID1 = 0;


    public Form1()
    {
        InitializeComponent();

        // for data source
        dtStopwatches.Columns.Add("Ticket", typeof(string));
        dtStopwatches.Columns.Add("Customer Name", typeof(string));
        dtStopwatches.Columns.Add("Area", typeof(string));
        dtStopwatches.Columns.Add("Instller", typeof(string));
        dtStopwatches.Columns.Add("Endorsed Date", typeof(string));
        dtStopwatches.Columns.Add("ID", typeof(int));
        dtStopwatches.Columns.Add("ID1", typeof(int));
        dtStopwatches.Columns.Add("BBX", typeof(string));
        dtStopwatches.Columns.Add("BBZ / BBC", typeof(string));
        dataGridView1.DataSource = dtStopwatches;


        // create 4 buttons everytime another row is added. 
        foreach (string buttonName in new string[] { "Start BBX", "Pause BBX", "StartBBZ", "Pause BBZ" })
        {
            DataGridViewButtonColumn colTemp = new DataGridViewButtonColumn();
            colTemp.Name = buttonName + "Col";
            colTemp.HeaderText = buttonName;
            colTemp.Width = 50;
            dataGridView1.Columns.Add(colTemp);
        }

        timer1.Tick += (timer1_Tick);
        timer1.Interval = 50;
        timer1.Start();

        timer2.Tick += (timer2_Tick);
        timer2.Interval = 50;
        timer2.Start();

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex > -1)
        {
            Stopwatch swTemp = swDct[(int)dataGridView1.Rows[e.RowIndex].Cells["ID1"].Value];
            switch (dataGridView1.Columns[e.ColumnIndex].HeaderText)
            {
                case "Start BBX":
                    swTemp.Start();

                    break;
                case "Pause BBS":
                    swTemp.Stop();
                    break;               
            }
        }

        if (e.RowIndex > -1)
        {
            Stopwatch Temp = wDct[(int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value];
            switch (dataGridView1.Columns[e.ColumnIndex].HeaderText)

            {
                case "Start BBZ":
                    Temp.Start();
                    break;
                case "Pause BBZ":
                    Temp.Stop();
                    break;
            }
        }

    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        foreach (DataRow dRow in dtStopwatches.Rows)
        {
            string elapsedString = swDct[(int)dRow["ID1"]].Elapsed.ToString(@"hh\:mm\:ss");
            dRow["BBX"] = elapsedString;

        }
        timer1.Start();
    }


    private void timer2_Tick(object sender, EventArgs e)
    {
        timer2.Stop();
        foreach (DataRow dRow in dtStopwatches.Rows)
        {

            string elapsed = wDct[(int)dRow["ID"]].Elapsed.ToString(@"hh\:mm\:ss");
            dRow["BBZ / BBC"] = elapsed;
        }
        timer2.Start();
    }

    private void btnAddStopwatch_Click(object sender, EventArgs e)
    {
        dtStopwatches.Rows.Add(txt_tn.Text, txt_cn.Text, txt_area.Text, txt_installer.Text, txt_date.Text, nextID, nextID1, "00:00:00");
        swDct.Add(nextID, new Stopwatch());
        wDct.Add(nextID, new Stopwatch());


        nextID++;
        nextID1++;

        txt_area.Text = "";
        txt_cn.Text = "";
        txt_date.Text = "";
        txt_installer.Text = "";
        txt_tn.Text = "";
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        //================
       dataGridView1.Columns["ID"].Visible = false;
       dataGridView1.Columns["ID1"].Visible = false;

    }

    private void btn_delete_Click(object sender, EventArgs e)
    {

        dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

    }

}

}

2 个答案:

答案 0 :(得分:0)

计时器用于显示已用时间...您为什么要这样做呢?

来自MSDN

Public method   Timer() 

初始化Timer类的新实例。

Public method   Timer(IContainer)   

使用指定的容器初始化Timer类的新实例。

所以......不:)

答案 1 :(得分:0)

StopWatch旨在衡量时间,所以你不能这样做。

您可以在其上创建一个包装器并执行类似的操作。

public class MyStopWatch
{
    private TimeSpan offset;
    private Stopwatch stopwatch = new Stopwatch();
    public MyStopWatch(TimeSpan offset)
    {
        this.offset = offset;
    }

    public TimeSpan Elapsed { get { return stopwatch.Elapsed + offset; } }
    public bool IsRunning { get { return stopwatch.IsRunning; } }

    public void Reset()
    {
        stopwatch.Reset();
    }
    public void Restart()
    {
        stopwatch.Restart();
    }
    public void Start()
    {
        stopwatch.Start();
    }
    public void Stop()
    {
        stopwatch.Stop();
    }
}

用例

var mySw =new MyStopWatch(TimeSpan.FromSeconds(15));
mySw.Start();
mySw.Elapsed//00:00:15