datagridview中的到期日期需要通知

时间:2018-09-23 10:12:18

标签: c# windows winforms

我非常需要此建议,因为我不熟悉此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天即将到期的商品

here

3 个答案:

答案 0 :(得分:0)

1,将新的列调用过期状态添加到您的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
               }
            }

答案 1 :(得分:0)

您可以尝试以下操作:

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;
    }
}

输出:

enter image description here

答案 2 :(得分:0)

嗨,乔纳森(Jonathan)试试这个例子

edg <- read.csv(header = TRUE, colClasses = 'character', text = '
Gene1,Gene2,Prob
1,22,3
2,22,6
3,22,6
4,22,9
5,22,3
6,22,4
7,22,8
8,22,4
9,22,6
10,22,8
11,22,6
12,22,10
13,22,6
14,22,3
15,22,6
16,22,6
17,22,0
18,22,4
19,22,6
20,22,4
')

vert <- read.csv(header = TRUE, colClasses = 'character', text = '
Symbol,Chr,Expr
1,21,9
2,17,10
3,17,0
4,20,0
5,6,9
6,5,11
7,12,0
8,1,20
9,17,11
10,17,7
11,17,11
12,10,0
13,17,0
14,7,7
15,17,6
16,17,0
17,2,5
18,5,10
19,17,10
20,17,9
21,12,4
22,3,2
')

# cast to numeric just to be sure
edg$Gene1 <- as.numeric(edg$Gene1)
edg$Gene2 <- as.numeric(edg$Gene2)

# adjust the indices so they're "0-based"
edg$Gene1 <- edg$Gene1 - 1
edg$Gene2 <- edg$Gene2 - 1

# Nodesize is also necessarily numeric
vert$Expr <- as.numeric(vert$Expr)

library(networkD3)
forceNetwork(Links = edg, Nodes = vert, Source = "Gene1", Target = "Gene2", 
         Value = "Prob", NodeID = "Symbol", Group = "Chr", opacity = 0.7,
         Nodesize = "Expr", zoom = TRUE, legend = TRUE)