winforms 从列表视图中检索多个值

bxpogfeg  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(854)

我有一个列表视图,我想循环查看它,并确定某个操作是否在特定时间完成,在本例中,该操作是阅读一本书。如果该特定书的“ReadByThisTime”列中的时间已经过去,我希望显示一条消息。我如何循环查看列表视图,以确保每本书都已按时阅读?Image of Listview

private void filllistview() //This is what is called to populate the listview
        {
            SqlConnection conn = new SqlConnection(@"conn");
            using (SqlDataAdapter sda = new SqlDataAdapter(@"SELECT * FROM Books", conn))
            {
                //Fill the DataTable with records from Table.
                System.Data.DataTable dt = new System.Data.DataTable();
                sda.Fill(dt);

                //Loop through the DataTable.
                foreach (DataRow row in dt.Rows)
                {
                    //Add Item to ListView.
                    ListViewItem item = new ListViewItem(row["Books"].ToString());
                    item.SubItems.Add(row["ReadByThisTime"].ToString());
                    item.SubItems.Add(row["ReadAt"].ToString());
                    listView1.Items.Add(item);
                }
            }
        }

           /*   This is what I want my listview to perform

               foreach(ListViewItem item in listView1.Items)
            {
                if(book is not read by time)
                {
                    MessageBox.Show("Error")
                }
            }*/
tcbh2hod

tcbh2hod1#

我会这样做。期限只有时间信息而没有日期,这有点奇怪。所以在下面的例子中,我只比较日期时间变量的时间部分。

private void alertForReadViolation() {
        //This is what I want my listview to perform

        foreach(ListViewItem item in listView1.Items)
        {
            //Datetime variables to hold datetimes parsed from listview strings.
            //If you don't initialize with value, complier complains that the variables aren't assigned to
            //  in the main comparison.
            DateTime deadline = DateTime.MaxValue; 
            DateTime readTime = DateTime.MinValue;
            
            //Try to parse the deadline from the listview text.
            if (!DateTime.TryParse(item.SubItems[1].Text, out deadline)) {
                Console.WriteLine("Could not parse Read Deadline from list view string.");
                return;
            }
            
            //Try to parse the readAt from the listview text.
            if (!DateTime.TryParse(item.SubItems[2].Text)) {
                Console.WriteLine("Could not parse ReadAt datetime from list view string.");
                return;
            }
            
            //Compare only the time portion of the date variables since the deadline only contains time information.
            if(TimeSpan.Compare(readTime.TimeOfDay, deadline.TimeOfDay) > 0)
            {
                MessageBox.Show("Error")
            }
        }
    }

或者,您可以在加载列表视图时进行比较。如果您直接从数据库DataRow获取日期时间数据,那么它将是强类型的,您不必从字符串解析日期/时间。那么,您的主填充函数将如下所示:

private void filllistview() //This is what is called to populate the listview
    {
        SqlConnection conn = new SqlConnection(@"conn");
        using (SqlDataAdapter sda = new SqlDataAdapter(@"SELECT * FROM Books", conn))
        {
            //Fill the DataTable with records from Table.
            System.Data.DataTable dt = new System.Data.DataTable();
            sda.Fill(dt);

            //Loop through the DataTable.
            foreach (DataRow row in dt.Rows)
            {
                //Add Item to ListView.
                ListViewItem item = new ListViewItem(row["Books"].ToString());
                
                //Get deadline data from database datetime field.
                DateTime deadline = (DateTime)row["ReadByThisTime"];
                item.SubItems.Add(deadline.ToString());
                
                //Get ReadAt data from database datetime field.
                DateTime readTime = (DateTime)row["ReadAt"];
                item.SubItems.Add(readTime.ToString());
                
                listView1.Items.Add(item);
                
                //Compare only the time portion of the date variables since the deadline only contains time information.
                if(TimeSpan.Compare(readTime.TimeOfDay, deadline.TimeOfDay) > 0)
                {
                    MessageBox.Show("Error")
                }
            }
        }
    }

相关问题