【问题标题】:Problem with Countdown to expire function倒计时到期功能的问题
【发布时间】:2022-01-12 23:32:06
【问题描述】:

我想在我的项目倒计时到期列中添加。我已经创建了医学数据库。

我正在为药物过期倒计时而苦苦挣扎,但它没有显示正确的过期天数。最后一栏。我在图片上展示了它:

我的过期日期功能:

public void UpdateCountdown()
{
    DateTime d1;
    DateTime d2;
    for (int i = 0; i < dataGridView1.RowCount - 1; i++)
    {
        d1 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); // date now
        d2 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[5].Value); // expiry date column
        TimeSpan ts = d2- d1;

        dataGridView1.Rows[i].Cells[9].Value = ts.Days;
        string Query3 = "Update Medicine set DaysLeft ='" + ts.Days + "'" + " where rowid ='" + dataGridView1.Rows[i].Cells[0].Value + "'";
        ExcecuteQuery(Query3);
        LoadData();
    }
}   

【问题讨论】:

  • “出了点问题” 怎么了?我看过图片,但我不知道是什么问题
  • 问题是最后一列倒计时在天后过期
  • 它没有显示正确的过期天数
  • 我已经更正了我的问题
  • 使用调试器查看 d1、d2 和 ts 的值。使用参数避免sql注入和格式错误。

标签: c#


【解决方案1】:

考虑使用the following code SO 答案,按照以下方式完成。

public class DateHelper
{
    public static string CalculateExpirationTime(DateTime expiryDate)
    {
        var currentDate = DateTime.Now;
        var dateDifference = (expiryDate - currentDate);

        if (dateDifference.Days >= 1)
            return $"{ dateDifference.Days } day(s) remained";
        else if (dateDifference.Hours >= 1)
            return $"{ dateDifference.Hours } hour(s) remained";
        else if (dateDifference.Minutes >= 1)
            return $"{ dateDifference.Minutes } minute(s) remained";
        else if (dateDifference.TotalSeconds >= 1)
            return $"{ dateDifference.Seconds } second(s) remained";

        return "Expired!";
    }
}

在这种情况下按钮代码输出到 Visual Studio 的输出窗口的表单代码。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private readonly BindingSource _bindingSource = new BindingSource();
    private void Form1_Load(object sender, EventArgs e)
    {
        var table = new DataTable();
        table.Columns.Add("NameColumn", typeof(string));
        table.Columns.Add("DateColumn", typeof(DateTime));
        table.Columns.Add("ExpireColumn", typeof(string));

        table.Rows.Add("Jane", new DateTime(2021,12,1));
        table.Rows.Add("Mike", new DateTime(2021,12,10));
        table.Rows.Add("Karen", new DateTime(2021,12,23));
        table.Rows.Add("Anne", new DateTime(2022,1,1));

        for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
        {
            table.Rows[rowIndex].SetField("ExpireColumn", 
                DateHelper.CalculateExpirationTime(
                    table.Rows[rowIndex].Field<DateTime>("DateColumn")));
        }

        _bindingSource.DataSource = table;
        dataGridView1.DataSource = _bindingSource;

    }
    private void IterateButton_Click(object sender, EventArgs e)
    {
        DataTable table = (DataTable)_bindingSource.DataSource;

        for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
        {
            Console.WriteLine(table.Rows[rowIndex]["ExpireColumn"]);
        }
    }
}

【讨论】:

  • 非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 2021-05-26
  • 2022-06-19
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多