using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace M423project { public partial class FormWarning : Form { private List _listAll = new List(); private List _listTemp; public FormWarning() { InitializeComponent(); grid.AutoGenerateColumns = false; BindCombobox(); } private void BindCombobox() { WarningData warningData = new WarningData(); _listAll.Add(new WarningConfigure { Val = 0, Content = "全部" }); _listAll.AddRange(warningData.GetWarningType(null)); cboType.DisplayMember = "Content"; cboType.ValueMember = "Val"; cboType.Items.AddRange(_listAll.ToArray()); cboType.SelectedItem = _listAll[0]; } private void btnQuery_Click(object sender, EventArgs e) { if ((dtBegin.Value.Date - dtEnd.Value.Date).Days > 0) { MessageBox.Show("开始时间不得大于结束时间"); return; } StringBuilder sb = new StringBuilder(); sb.AppendFormat(@"SELECT warnConfigure.Content Type,warn.Time,warn.ClearTime FROM Warning warn LEFT JOIN WarningConfigure warnConfigure ON warn.Type=warnConfigure.Val WHERE warn.Time BETWEEN '{0}' AND '{1}'", dtBegin.Value.ToString("yyyy-MM-dd 00:00:00"), dtEnd.Value.ToString("yyyy-MM-dd 23:59:59")); WarningConfigure selected = cboType.SelectedItem as WarningConfigure; if (selected != null && selected.Val != 0) { sb.AppendFormat(@" AND Type={0}", selected.Val); } sb.AppendFormat(" order by warn.Time"); DataTable dt = CommonUtil.mainForm.DetectData.Query(sb.ToString()); grid.DataSource = dt; grid.Columns["Time"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss"; grid.Columns["ClearTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss"; } private void cboType_TextUpdate(object sender, EventArgs e) { cboType.Items.Clear(); _listTemp = new List(); if (cboType.Text == "") { cboType.Items.AddRange(_listAll.ToArray()); return; } foreach (var item in _listAll) { if (item.Content.Contains(cboType.Text)) { _listTemp.Add(item); } } cboType.Items.AddRange(_listTemp.ToArray()); cboType.SelectionStart = cboType.Text.Length; Cursor = Cursors.Default; cboType.DroppedDown = true; } } }