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;

转载于:https://www.cnblogs.com/bingsying/p/7851362.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值