テーブルを作成してデータを追加する

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();
285件の元の記事を公開 50件の賞賛 450,000回の閲覧+

おすすめ

転載: blog.csdn.net/lzh657083979/article/details/105603999