0
我需要在c#中添加向上計數器到DataGridView
。當我運行時,它只會在datagridview
的最後一行的單元格中計數值。的所有其它細胞在計時器列顯示零如何添加計數器到datagridview單元格C#
private void fillTodayPatientsTable()
{
string connectionString = "Server=localhost\\sqlexpress;Database=dental;User Id=sa;Password=abc123;";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
string id = "";
string firstname = "";
string lastname = "";
string startTime = "";
DateTime attendedTime = DateTime.Now;
string oString = "Select id,firstname,lastname,start_time,attended_time from Appointments";
SqlCommand oCmd = new SqlCommand(oString, myConnection);
myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
if (oReader.HasRows)
{
DataTable dttable = new DataTable();
DataColumn column;
column = new DataColumn();
// column.DataType = System.Type.GetType("System.Int32");
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Firstname";
dttable.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Lastname";
dttable.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Start time";
dttable.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Attended time";
dttable.Columns.Add(column);
lbl.ForeColor = Color.Black;
lbl.Font = new Font("TimesRoman", 12, FontStyle.Bold);
dgStyle.GridColumnStyles.Add(dgLabel);
dgLabel.HeaderText = "Label Column";
dgLabel.MappingName = "Name";
dgLabel.Width = 200;
dgStyle.PreferredRowHeight = 24;
dttable.Columns.Add("Timer");
labelll = new Label();
col = col = new DataGridViewLabelCellColumn();
int counter = 0;
while (oReader.Read())
{
id = oReader["id"].ToString();
firstname = oReader["firstname"].ToString();
lastname = oReader["lastname"].ToString();
startTime = oReader["start_time"].ToString();
attendedTime = (DateTime) oReader["attended_time"];
double attendedTimeStamp= (attendedTime - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
double DateNowTimeStamp = (DateTime.Now - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
int waitingTime =(int) (DateNowTimeStamp - attendedTimeStamp);
row = dttable.NewRow();
row["Firstname"] = firstname;
row["Lastname"] = lastname;
row["Start time"] = startTime;
row["Attended time"] = attendedTime;
row["Timer"] = (waitingTime+counter).ToString();
timer1.Tick += new EventHandler(timer1_Tick);
dttable.Rows.Add(row);
countTest++;
timer1 = new Timer();
timer1.Interval = 1000; // 1 second
timer1.Start();
dgvTodayPatient.DataSource = dttable;
}
}
myConnection.Close();
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
if (counter ==60*60*24)
timer1.Stop();
row["Timer"] = counter.ToString();
}