C# 開発

【C#】DataGridView

2025.01.20 DataGridView : セルを読み取り専用にする

private void button1_Click(object sender, EventArgs e)
{
    var rows = dataGridView1.Rows.Cast().AsEnumerable().Where(row => string.IsNullOrEmpty((row.Cells[iColPos].Value ?? "").ToString()) == false);
    foreach (DataGridViewRow row in rows)
    {
        row.Cells[iColPos].ReadOnly = true;
        row.Cells[iColPos].Style.ForeColor = Color.Gray;
    }
}

2025.01.22 DataGridView : グリッド線の色を変更する

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.GridColor = Color.FromKnownColor(KnownColor.ControlDark);
}

2025.01.25 DataGridView : ソートする

private void button1_Click(object sender, EventArgs e)
{
    // データグリッドビューの 2 番目の列で昇順にソートしています。
    // ソート後にカレントセルが見えなくなる可能性があるので、左上のセルをカレントに設定しています。
    dataGridView1.Sort(dataGridView1.Columns[1], System.ComponentModel.ListSortDirection.Ascending);
    dataGridView1.CurrentCell = dataGridView1[0, 0];
}

2025.01.25 DataGridView : イメージカラムを追加する

private void button1_Click(object sender, EventArgs e)
{
    // データグリッドビューイメージカラムを追加して、
    // サンプルピクチャーから読み込んだイメージファイルを追加しています。
    dataGridView1.Columns.Add(new DataGridViewImageColumn());
    dataGridView1.Rows.Add(System.Drawing.Image.FromFile(@"C:\Windows\Web\Wallpaper\Windows\img0.jpg"));
}

2025.01.25 DataGridView : 計算式を設定して自動計算させる

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1[0, e.RowIndex].Value is null) return;
    if (dataGridView1[1, e.RowIndex].Value is null) return;

    // データグリッドビューの列 A と列 B の和を 列 (SUM) に自動計算して設定しています。
    // 数字が入力されるたびに自動計算を実行するには、CellFormatting イベントを使います。
    int.TryParse((string)(dataGridView1[0, e.RowIndex].Value ?? ""), out int v1);
    int.TryParse((string)(dataGridView1[1, e.RowIndex].Value ?? ""), out int v2);

    dataGridView1[2, e.RowIndex].Value = v1 + v2;
}

2025.01.25 DataGridView : ボタンカラムを挿入する

private void Form1_Load(object sender, EventArgs e)
{
    // データグリッドビューの最初の列として、ボタンカラムを追加しています。
    // Insert メソッドには追加したい列番号を指定します。
    dataGridView1.ColumnCount = 3;
    dataGridView1.RowCount = 5;

    var dgvButton = new DataGridViewButtonColumn();
    dgvButton.Name = "チェック";
    dgvButton.HeaderText = "チェック";
    dgvButton.Text = "チェック";
    dgvButton.UseColumnTextForButtonValue = true;

    dataGridView1.Columns.Insert(0, dgvButton);
}

2025.01.25 DataGridView : ボタンカラムを追加する

private void Form1_Load(object sender, EventArgs e)
{
   // データグリッドビューの最後の列として、ボタンカラムを追加しています。
   // また、ボタンカラムがクリックされた時に動作するイベントも記述しています。
    dataGridView1.ColumnCount = 3;
    dataGridView1.RowCount = 5;

    var dgvButton = new DataGridViewButtonColumn();
    dgvButton.Name = "チェック";
    dgvButton.HeaderText = "チェック";
    dgvButton.Text = "チェック";
    dgvButton.UseColumnTextForButtonValue = true;

    dataGridView1.Columns.Add(dgvButton);
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 3)
    {
        MessageBox.Show("チェック完了");
    }
}

-C# 開発