using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
List<Student> students = new List<Student>();
Student hat = new Student("Hathaway");
Student peter = new Student("Peter");
Student dell = new Student("Dell");
Student anne = new Student("Anne");
students.Add(hat);
students.Add(peter);
students.Add(dell);
students.Add(anne);
this.dataGridView1.DataSource = students;
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = "true";//如果为true则为选中,false未选中
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
int index = dataGridView1.CurrentRow.Index;
if(e.RowIndex==-1)
{
dataGridView1_CellClick(sender, e);
return;
}
this.dataGridView1.Rows[e.RowIndex].Selected = true;
if (Convert.ToBoolean(dataGridView1.Rows[index].Cells[0].Value))
{
dataGridView1.Rows[index].Cells[0].Value = false;
}
else
{
dataGridView1.Rows[index].Cells[0].Value = true;
////其他的都是false
//for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
//{
// if (i != index)
// {
// dataGridView1.Rows[i].Cells[0].Value = false;
// }
//}
}
}
catch
{
}
}
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
}
bool checkeding = false;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1 &&!checkeding)//如果单击列表头,全选.
{
int i;
for (i = 0; i < this.dataGridView1.RowCount; i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = "true";//如果为true则为选中,false未选中
}
checkeding = true;
}
else if (e.RowIndex == -1 && checkeding)//如果单击列表头,全选.
{
int i;
for (i = 0; i < this.dataGridView1.RowCount; i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = "false";//如果为true则为选中,false未选中
}
checkeding = false;
}
}
}
internal class Student
{
public Student(string name)
{
Name = name;
}
public string Name
{
get;
set;
}
}
}
DataGridView 全选
猜你喜欢
转载自blog.csdn.net/softuse/article/details/89925622
今日推荐
周排行