ListView 和 Combobox

listview控件:

1、listview设置列标题:
this.listview1.Columns.Add(" ", 45, HorizontalAlignment.Left);
this.listview1.Columns.Add("车牌号码", 120, HorizontalAlignment.Left);
this.listview1.Columns.Add("开始时间", 150, HorizontalAlignment.Left);
this.listview1.Columns.Add("终止时间", 150, HorizontalAlignment.Left);

2、listview绑定数据:
ListViewItem item = new ListViewItem();
item.Text = NumList.ToString();
item.SubItems.Add(vehNum, Color.Blue, Color.White, new System.Drawing.Font("微软雅黑", 10, FontStyle.Bold));//修改字体颜色并加粗
item.SubItems.Add("");
item.SubItems.Add("");
item.UseItemStyleForSubItems = false;//如果给表格中的某个字段设置了样式,则必须等于false
this.listview1.Items.Add(item);

 

3、注意:如果表格被重复使用,在每次运行前要对表数据进行清除与刷新:

listview1.Items.Clear();
listview1.Columns.Clear();
listview1.Refresh();

4、在每项旁边显示复选框

lvVehGrid.CheckBoxes = true;

5、获取点击选择的值
string vehNum = this.lvVehGrid.CheckedItems[0].SubItems[1].Text; //获取车牌号

6、指定listview只能单选选择一行,需要两个事件:

private void lvVehGrid_ItemCheck(object sender, ItemCheckEventArgs e)
{
  if (!lvVehGrid.Items[e.Index].Checked)//如果点击的CheckBoxes没有选中
  {
    foreach (ListViewItem lv in lvVehGrid.Items)
    {
      if (lv.Checked)//取消所有已选中的CheckBoxes
      {
        lv.Checked = false;
        lv.Selected = false;
      }
    }
    lvVehGrid.Items[e.Index].Selected = true;
  }
}

private void lvVehGrid_SelectedIndexChanged(object sender, EventArgs e)
{
  foreach (ListViewItem lv in lvVehGrid.Items)
  {
    if (lv.Selected)
      lv.Checked = true;
    else
    {
      if (lvVehGrid.SelectedIndices.Count > 0)
      {
        if (lv.Checked)
        lv.Checked = false;
      }
    }
  }
}

--------------------------------------------------------------------------------------

ComboBox控件

1、绑定源数据
string[] date = {"统计里程","统计停车时间","油量曲线","告警记录查询"};

combobox1.DataSource = date;

2、获取combox被选着的索引

nOldSt = combobox1.SelectedIndex;

posted @ 2017-11-17 14:15  冰冰影  阅读(226)  评论(0编辑  收藏  举报