This article focuses on C # to add data from access to a time period condition query in the ListView, and the friends you need can refer to the following
First, let the ListView control display the table header method
In the form, add the ListView space to its properties in the settings: The View property is set to: Detail,columns the text in the header of the collection.
Second, use the code to add item to the ListView.
First, the Item property of the ListView includes items and subitems. You must instantiate a Listiteview object first. Specific as follows:
ListViewItem listviewitem=new ListViewItem (); Listviewitem.subitems[0]. Text= "" 11111;//the value of the first example of the first line LISTVIEWITEM.SUBITEMS.ADD ("222");///listviewitem.subitems.add ("222");/// etc. ListView1.Items.Add (ListViewItem);
III. SQL Statement Writing specification for time period queries in Access (with Datetimepick control)
Note: # must be added.
String Sql=select * from TableName where Timefield between # "+datetimepick1.value.tostring () +" # and # "+ DateTimePick2.vlaue.ToString () + "#";
Iv. connecting to the database, querying data conditionally and displaying it in the ListView
String path = System.Environment.CurrentDirectory + "\\database.mdb"; OleDbConnection con = new OleDbConnection ("Provider=microsoft.ace.oledb.12.0;data source=" + path);//station2.mdb con . Open (); String sql = "SELECT * from Sendrecord where sendtime between #" + dateTimePicker1.Value.ToString () + "# and #" + DateTime Picker2.Value.ToString () + "#"; String sql = "SELECT * from Sendrecord"; OleDbDataAdapter da = new OleDbDataAdapter (sql, con); DataSet dt = new DataSet (); Da. Fill (DT); DataTable DTB = dt. Tables[0]; foreach (DataRow row in DTB. Rows) {ListViewItem ListViewItem = new ListViewItem (); ListviewItem.SubItems.Clear (); Listviewitem.subitems[0]. Text = string. Format ("{0:yyyy-mm-dd hh:mm}", row["Sendtime"]); LISTVIEWITEM.SUBITEMS.ADD ((String) row["SENDER"); LISTVIEWITEM.SUBITEMS.ADD (String) row["CONTENT"); LISTVIEWITEM.SUBITEMS.ADD (String. Format ("{0}", row["AUDITOR")); ListViewItem.SubItems.Add (String. Format ("{0:yyyy-mm-dd hh:mm}", row["Audittime"]); LISTVIEWITEM.SUBITEMS.ADD (String. Format ("{0}", row["Auditstatus")); LISTVIEW1.ITEMS.ADD (ListViewItem); } con. Close ();}