【问题标题】:notification needed for expiry date in datagridviewdatagridview中的到期日期需要通知
【发布时间】:2018-09-23 10:12:18
【问题描述】:

我非常需要这方面的建议,因为我不熟悉这个 foreach 声明。因此,我需要为库存中的过期物品弹出通知。至少在商品即将到期前 7 天,系统会为用户弹出通知。所以,我在这里尝试了代码,但它还没有完成。

foreach (DataGridViewRow row in InventoryList.Rows)
{
    foreach (DataGridViewColumn col in InventoryList.Columns)
    {
        InventoryList.Rows.[4].Cells[col.Index].Value =
    }

    MessageBox.Show("Nearing Expiry" + "@ItemName");        
}

这是系统,所以我需要知道哪些物品在到期前至少 7 天即将到期

【问题讨论】:

  • 嗨乔纳森..您能否提供更多关于您需要检查或比较哪些列的信息..
  • 我需要检查到期日期列(在链接中),并且我需要知道如何在该列到期之前收到通知
  • InventoryList 是否绑定到 DataTable?您是否需要单独的过期项目列表,或者现有DataGridView 上的视觉指示就足够了?

标签: c# windows winforms


【解决方案1】:

你可以试试这样的:

var nowPlusOneWeek = DateTime.Now + new TimeSpan(7, 0, 0, 0);

var expiringItems = InventoryList.Rows.Cast<DataGridViewRow>().
    Where(x => x.Cells[0].Value != null && Convert.ToDateTime(x.Cells[3].Value.ToString()) <= nowPlusOneWeek);

var Expiry = new StringBuilder();
foreach (var item in expiringItems)
    Expiry.Append("Nearing Expiry: " + item.Cells[0].Value.ToString() + "\n");

MessageBox.Show(Expiry.ToString());

更新:

这里有更多代码可以在一些随机数据上进行测试:

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

        FillTable();
    }

    private void FindBT_Click(object sender, EventArgs e)
    {
        FindExpiring();
    }

    private void FindExpiring()
    {
        var nowPlusOneWeek = DateTime.Now + new TimeSpan(7, 0, 0, 0);

        var expiringItems = InventoryList.Rows.Cast<DataGridViewRow>().
            Where(x => x.Cells[0].Value != null &&
            DateTime.ParseExact(x.Cells[3].Value.ToString(), "dd/MM/yyyy",
            CultureInfo.InvariantCulture) <= nowPlusOneWeek);

        var Expiry = new StringBuilder();
        foreach (var item in expiringItems)
            Expiry.Append("Nearing Expiry: " + item.Cells[0].Value.ToString() + "\n");

        MessageBox.Show(Expiry.ToString());
    }

    void FillTable()
    {
        Random RND = new Random();
        DataTable dt1 = new DataTable();

        dt1.Columns.Add("InventoryID", typeof(int));
        dt1.Columns.Add("ItemID", typeof(int));
        dt1.Columns.Add("Quantity", typeof(int));
        dt1.Columns.Add("ExpityDate", typeof(string));

        for (int i = 0; i < 10; i++)
        {
            DataRow dr1 = dt1.NewRow();
            dr1["InventoryID"] = i;
            dr1["ItemID"] = 1;
            dr1["Quantity"] = 20;
            dr1["ExpityDate"] = (DateTime.Now + new TimeSpan(RND.Next(20), 0, 0, 0)).ToString("dd/MM/yyyy");
            dt1.Rows.Add(dr1.ItemArray);
        }
        InventoryList.DataSource = dt1;
    }
}

输出:

【讨论】:

  • 我认为这已经接近我需要做的事情了,但由于没有来自库存的通知,即使我在过期前 2 天输入了一些项目,它仍然不起作用
【解决方案2】:

1, 将新列调用 Expired Status 添加到您的 datagridview。

2,

foreach(DataGridViewRow item in dgvDenominations.Rows)
            {
               if (DateTime.Now.AddDays(7).Equals(DateTime.Parse(item.Cells["yourdateexpriedcellname"].Value.ToString())))
               {
                   item.Cells["ExpiredStatus"].Value = "Nearing Expiry";
                   // orr message if you want
               }
            }

【讨论】:

  • 我试过了,但我认为它不适用于我当前的项目
【解决方案3】:

嗨乔纳森试试这个例子

DataGridView grd = new DataGridView();
            grd.Columns.Add("Id", "Id");
            grd.Columns.Add("citemName", "Item Name");
            grd.Columns.Add("dExipryDate", "Expires On");


            grd.Rows.Add("1", "Soap", "21 august 2018");
            grd.Rows.Add("2", "Soap2", "21july 2018");
            grd.Rows.Add("3", "Soap3", "30 august 2018");
            grd.Rows.Add("4", "Soap4", "27 june 2018");
            grd.Rows.Add("5", "Soap5", "21 march 2018");
            grd.Rows.Add("6", "Soap6", "28 september 2018");
            grd.Rows.Add("7", "Soap7", "23 october 2018");



            foreach (DataGridViewRow drow in grd.Rows)            {

                DateTime expdate = DateTime.Parse(drow.Cells["dExipryDate"].Value.ToString());

                TimeSpan tmspn = expdate - DateTime.Now.Date;
                if (tmspn.Days < 7 && tmspn.Days >0)
                {
                    //tmspn.Days >0 if you want to show already expired item 
                    MessageBox.Show(drow.Cells["dExipryDate"].Value.ToString() + "Will Expire in " + tmspn.Days + "Days");
                }

            }

【讨论】:

  • 我不能这样设置,因为商品的保质期会有所不同,而且产品不受约束,因为它是由食物组成的
猜你喜欢
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多