DataGridViewRow

DataGridViewの行を表します。

クラス階層

  • System.Object
    • System.Windows.Forms.DataGridViewElement

コンストラクタ

DataGridView.RowTemplateを使用せずに、空の状態でインスタンスを作成できます。

public DataGridViewRow ();
DataGridViewRow コンストラクター (System.Windows.Forms) | Microsoft Learn

RowTemplateを使用するならばコンストラクタを用いず、DataGridViewRowCollection.Add()で追加するか、テンプレートをClone()で複製して用います。

DataGridViewRow row = (DataGridViewRow)dataGridView.RowTemplate.Clone();

プロパティ

プロパティ 内容
DataGridView DataGridView この要素に関連付けられているDataGridView。関連付けられていなければnull
int Index DataGridView内での相対位置。既定は-1で、行がDataGridViewに関連付けられていないことを示す。DataGridViewColumn.Indexとは異なり、表示位置と一致する
DataGridViewRowHeaderCell HeaderCell 行のヘッダーセル。典型的には行ラベルを表示するのに用いられる。設定によってはクリックで行を選択できる

この行のヘッダーセルのスタイルなどは、DataGridViewのRowHeaders***プロパティで設定できる

DataGridViewCellCollection Cells 行のセル (DataGridViewCell) のコレクション
bool Displayed trueならば、画面に表示されている。一部でも表示されていればtrueとなる

内部的にはGetState()で、DataGridViewElementStates.Displayedであることで判定している Displayed - DataGridViewRow.cs

bool Selected trueならば、行が選択されている
bool Frozen trueならば、行が固定 (frozen) されている

固定されている行より上のすべての行は、スクロール時に固定される

DataGridViewElementStates State 行の状態
DataGridViewCellStyle DefaultCellStyle 行の既定のスタイル
int Height 行の高さ
int MinimumHeight 行の最小の高さ
DataGridViewTriState Resizable 行をリサイズ可能か、AllowUserToResizeRowsがtrueのときだけリサイズ可能かどうか
object DataBoundItem 行に結びつけられているオブジェクト

DataSourceが設定されているとき、その行への参照を得られる

string ErrorText 行のエラーを表すエラーメッセージのテキスト
     
プロパティ - DataGridViewRow クラス (System.Windows.Forms) | Microsoft Learn

Cells

Cellsプロパティから列名でDataGridViewCellを取得するとき、それに該当するセルが存在しないとArgumentExceptionが投げられます。これに対処するにはDataGridView.Columns.Contains(string columnName)で、その列が存在するか先に確認します。

State

DataGridViewElementStates 列挙型
列挙子 数値 意味
Displayed 1 Indicates the an element is currently displayed onscreen.
Frozen 2 Indicates that an element cannot be scrolled through the UI.
None 0 Indicates that an element is in its default state.
ReadOnly 4 Indicates that an element will not accept user input to change its value.
Resizable 8 Indicates that an element can be resized through the UI. This value is ignored except when combined with the ResizableSet value.
ResizableSet 16 Indicates that an element does not inherit the resizable state of its parent.
Selected 32 Indicates that an element is in a selected (highlighted) UI state.
Visible 64 Indicates that an element is visible (displayable).
DataGridViewElementStates Enum (System.Windows.Forms) | Microsoft Learn

Height

  • ユーザーによる高さの変更を認めないならば、AllowUserToResizeRowsをfalseにする
  • 高さが自動で調整されるようにするには、DataGridView.AutoSizeRowsModeプロパティでNone以外を指定する。これを任意のタイミングで行うにはDataGridView.AutoResizeRows()を呼ぶ
  • Heightプロパティの値は、並べ替えが行われるとDataGridViewRow.RowTemplate.Heightの値に再設定される。よって既定の高さは、そのRowTemplateを介して設定する

並べ替え後もそれぞれの行の高さを維持するには、RowHeightInfoPushedイベントで値を保持しておき、RowHeightInfoNeededイベントでその値に設定するようにします。Remarks - DataGridView.RowHeightInfoNeeded Event (System.Windows.Forms) | Microsoft Learn

int[] height = new int[100];

private void dataGridView1_RowHeightInfoPushed(object sender, DataGridViewRowHeightInfoPushedEventArgs e)
{
    height[e.RowIndex] = e.Height;
}

private void dataGridView1_RowHeightInfoNeeded(object sender, DataGridViewRowHeightInfoNeededEventArgs e)
{
    e.Height = height[e.RowIndex];
}

列ヘッダーの高さ

列ヘッダーの高さは、DataGridView.ColumnHeadersHeightで設定します。ただしDataGridView.ColumnHeadersHeightSizeModeがAutoSizeに設定されているとこれが無視されるため、先にAutoSize以外に変更します。

メソッド

メソッド 機能
GetState(Int32) 行の状態を得られる
Paint(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Boolean, Boolean) 現在の行を描画できる
PaintCells(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Boolean, Boolean, DataGridViewPaintParts) 現在の行のセルを描画できる
PaintHeader(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Boolean, Boolean, DataGridViewPaintParts) 現在の行のヘッダーセルを描画できる
   
メソッド - DataGridViewRow クラス (System.Windows.Forms) | Microsoft Learn

DataGridViewとの連携

行の追加

// セルに設定するオブジェクトを指定して、行に追加
string[] values = new string[] { "A", "B" };
int rowIndex1 = dataGridView.Rows.Add(values);
int rowIndex2 = dataGridView.Rows.Add(10, 20);

// 行を追加して、その行のセルにオブジェクトを指定
int rowIndex3 = dataGridView.Rows.Add();
DataGridViewRow row = dataGridView.Rows[rowIndex3];

row.Cells[0].Value = 123;
row.Cells[1].Value = "ABC";

行の検索

データソースと結びつけているならば、それが備えるフィルタ機能で検索できます。さもなくばDataGridView.Rowsを1行ずつ調べます。c# - Find a row in dataGridView based on column and value - Stack Overflow

DataGridViewRowに結びつけられているデータソースの行は、DataBoundItemから取得できます。このことを利用すれば、データソースの行からDataGridViewRowを特定できます。

DataTable table = new DataTable();
table.Columns.Add();

DataRow row0 = table.Rows.Add("");
DataRow row1 = table.Rows.Add("");

dataGridView.DataSource = table;


object obj1 = dataGridView.Rows[1].DataBoundItem; // 行に結びつけられているオブジェクトを取得する
DataRowView rowView1 = (DataRowView)obj1;

bool res0 = object.ReferenceEquals(row0, rowView1.Row); // false
bool res1 = object.ReferenceEquals(row1, rowView1.Row); // true
Microsoft Learnから検索