public Form1()
{
InitializeComponent();
//不出现最后一行新增的一列
dataGridView1.AllowUserToAddRows = false;
//设置列数
dataGridView1.ColumnCount = 3;
//设置是否显示第一列
dataGridView1.ColumnHeadersVisible = true;
DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
dataGridViewCellStyle.BackColor = Color.Black;
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[1].Name = "Bir";
dataGridView1.Columns[2].Name = "Sex";
//手动添加一条数据
string[] str = new string[] { "11", "22", "33" };
dataGridView1.Rows.Add(str);
}
删除数据
删除选中的行
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
if (!r.IsNewRow)
{
dataGridView1.Rows.Remove(r);
}
}
删除所有的数据
dataGridView1.Rows.Clear();