9
20

More than 5 years have passed since last update.

DataGridViewの使い方

Posted at

DataGridViewに値をセットする方法

インデクサを使う

// "0"をセット
dataGridView1[0, 0].Value = 0.ToString();

Rowsプロパティを使う

// "0"をセット
dataGridView1.Rows[0].Cells[0].Value = 0.ToString();

DataSouceプロパティを使う

List<T>をまとめてDataGridViewにセットする。

Form1.cs
List<Product> procucts = new List<Product>();

procucts.Add(new Product(1, "米"));
procucts.Add(new Product(2, "そば"));

dataGridView1.DataSource = procucts;
Product.cs
class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Product(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

datagridview.PNG

DataGridViewに列を追加する方法

dataGridView1.Columns.Add("ID", "Product ID");
dataGridView1.Columns.Add("Name", "Product Name");

DataGridViewに行を追加する方法

// 空白行を追加
dataGridView1.Rows.Add();
// セル内容を指定して行を追加
dataGridView1.Rows.Add(new string[] { "1", "Good Product" });
9
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
20