public List<myist> list = new List<myist>();//创建一个单列集合 private void Form1_Load(object sender, EventArgs e) { //将他们都存在这个单列集合中 myist a = new myist(); a.hao = 20090101; a.name = "王小毛"; a.age = 19; a.Sex = "男"; myist a2 = new myist(); a2.hao = 20090102; a2.name = "周新宇"; a2.age = 19; a2.Sex = "男"; list.Add(a); list.Add(a2); dataGridView1.DataSource = list;//将单列集合中的数据连接到 dataGridView中。 }
public void clear(List<myist> list) { dataGridView1.DataSource = new BindingList<myist>(list);//刷新dataGridView数据。 } private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) { //新增一个 Form2 a = new Form2(); a.fmr = this; a.Show(); this.Hide(); }
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { string a= dataGridView1.SelectedCells[0].Value.ToString();//获取首行首列的值 for (int i = 0; i < list.Count; i++) { //遍历出list下标和dataGridView首行首列的值,让他们作比较,相等就删除。 int b = Convert.ToInt32 (a); if(list[i].hao==b) { list.RemoveAt(i);//删除 } dataGridView1.DataSource = new BindingList<myist>(list);//刷新dataGridView数据 } } //查看 private void button1_Click(object sender, EventArgs e) { List<myist> temp = new List<myist>(); //遍历出ID,根据工号模糊查询。 foreach (myist item in list) { string num=item.hao.ToString();//进行类型转换,因为hao号是int类型 //用string类的INdexOf()实现模糊查询。 if (num.IndexOf(this.textBox1.Text.Trim())!=-1) { temp.Add(item); } dataGridView1.DataSource = new BindingList<myist>(temp);//刷新 dataGridView窗体。 } }
public static Dictionary<string, Record> recordList = new Dictionary<string, Record>();//创建一个双列集合,将签到时间和签退时间存放在一个双列集合中。 private void 签到ToolStripMenuItem_Click(object sender, EventArgs e) { //查看是否选中一行。 if (dataGridView1.SelectedRows.Count != 1) { MessageBox.Show("请选中一行"); } string s = dataGridView1.SelectedCells[0].Value.ToString(); //遍历得到key的值,然后和dataGridView下标为id的值相匹配。 foreach (string hao in recordList.Keys ) { if(s==hao) { MessageBox.Show("已经签到"); } } Record a = new Record(); a.hao = s; a.Name = dataGridView1.SelectedCells[1].Value.ToString(); a.time1 = DateTime.Now;//获取签到的时间。 recordList.Add(a.hao,a); MessageBox.Show("签到成功"); } private void 签退ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.dataGridView1.SelectedRows.Count!=1) { MessageBox.Show("请选中一行"); } string hao = dataGridView1.SelectedCells[0].Value.ToString(); bool isout = false; foreach (string key in recordList.Keys) { if(key==hao){ recordList[key].time1 = DateTime.Now; MessageBox.Show("签退成功"); isout = true; break; } } if(!isout) { MessageBox.Show("抱歉签到未成功"); } }
时间: 2024-12-27 22:09:07