C # add data queried by time period conditions in Access to ListView,
C # add data queried by time period conditions in Access to ListView
1. How to display the table header in the ListView Control
Add a ListView space to the form. Set the attribute to "View" and add the text in the header in the Columns set.
2. Use the code to add an Item to ListView.
First, the Item attributes of ListView include Items and SubItems. A ListIteView object must be instantiated first. The details are as follows:
ListViewItem listViewItem = new ListViewItem (); listViewItem. subItems [0]. text = "" 11111; // The value of listViewItem In the first example of the first line. subItems. add ("222"); // listViewItem. subItems. add ("222"); // and so on ListView1.Items. add (listViewItem );
Iii. SQL statement writing specification for time period query in Access (using the dateTimePick Control)
Note: # It must be added.
string sql=select * from tableName where timeField between #"+dateTimePick1.value.ToString()+"# and #"+dateTimePick2.vlaue.ToString()+"#";
4. Connect to the database, query data by conditions and display it in 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 #" + dateTimePicker2.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();}