【C#】【Form】DataGridView ~ プロパティ編 ~

■ はじめに

 * DataGridView のプロパティが多いので、メモ。

■ DataSource

 * データ バインディングするのに使用
 * データ バインディングについては、以下の関連記事を参照。
binding ~ データバインディング
https://blogs.yahoo.co.jp/dk521123/21268762.html

サンプル

var dataTable = new DataTable();

dataTable.Columns.Add("ID", typeof(string));
dataTable.Columns.Add("Value", typeof(string));

dataTable.Rows.Add("X002", "Tokyo");
dataTable.Rows.Add("X001", "Osaka");

dataGridView1.DataSource = dataTable;

■ FirstDisplayedScrollingRowIndex

https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.datagridview.firstdisplayedscrollingrowindex?view=netframework-4.7.2
 * 表示される最初の行のインデックスを取得/設定

補足:その他のDataGridView 表示に関わるプロパティ

Rows.Displayed
https://dobon.net/vb/dotnet/datagridview/displayed.html
if (dataGridView1.Rows[0].Displayed)
{
  // 表示されている
}
Rows.GetRowCount(DataGridViewElementStates.Displayed)
https://qa.atmarkit.co.jp/q/7574
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q10140184875
// 表示されている行数
var displayedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Displayed);

■ CurrentCell

起動時にセルが選択されないようにするには

 * Shownイベントで「CurrentCell = null」する 
サンプル
private void Form1_Shown(object sender, EventArgs e)
{
  // 起動時にセルが選択されないようにする
  this.dataGridView1.CurrentCell = null;
}
実際のコードは、以下の関連記事を参照
https://blogs.yahoo.co.jp/dk521123/29362064.html

■ MaxInputLength

 * セル内の最大文字数
 * 既定値は 32767

■ RowHeadersVisible

左端のやつを消す場合

 * RowHeadersVisible = false

参考文献

http://hissoritobotobo.blog48.fc2.com/blog-entry-193.html

■ AutoSizeColumnsMode

 * 列(横)幅の自動調整

領域一杯まで広げるには...

 * AutoSizeColumnsMode = Fill

各項目の幅の割合を調整するには...

 * DataGridViewColumn.FillWeight : 各列の列幅の割合
  => 特に、100分率でなくてもいい(デフォルト値は100で振られてるし)

参考文献

http://tt195361.hatenablog.com/entry/2015/05/12/171702

■ ColumnHeadersHeight / ColumnHeadersHeightSizeMode

 * ヘッダーの高さの調整

使用上の注意

 * ColumnHeadersHeightSizeMode = EnableResizing を指定する必要がある
   (ColumnHeadersHeightSizeMode = AutoSize では、ColumnHeadersHeight を指定しても効かない)

参考文献

http://hiros-dot.net/CS2005/Control/DataGridView/DataGridView21.htm

■ SelectionMode

 * セルの選択方法を示す

行全体を選択状態にしたい場合

 * DataGridViewのプロパティを「SelectionMode:FullRowSelect」にする

サンプル

 以下の記事「DataGridView に右クリックを適用する」を参照のこと
https://blogs.yahoo.co.jp/dk521123/30488275.html

■ MultiSelect

 * 複数のセル、行、または列を同時に選択できるかどうかを示す

複数行選択できないようにする場合

 * DataGridViewのプロパティを「MultiSelect:false」にする
http://dobon.net/vb/dotnet/datagridview/singleselect.html

サンプル

 以下の記事「DataGridView に右クリックを適用する」を参照のこと
https://blogs.yahoo.co.jp/dk521123/30488275.html

■ DataBoundItem

 * 行にバインドされているオブジェクトの取得できる

サンプル

 以下の記事「DataGridView に右クリックを適用する」を参照のこと
https://blogs.yahoo.co.jp/dk521123/30488275.html

参考文献

http://d.hatena.ne.jp/gsf_zero1/20070315/p1

■ Dock

 * DataGridViewの境界領域を定義
 * Fill:領域一杯まで広げる

■ AllowUserToResizeColumns / AllowUserToResizeRows

 ユーザが行・列のサイズを変更できるかどうかを設定(True/False)

■ DefaultCellStyle.WrapMode

折り返して表示させるには...

 * DataGridViewTriState.Trueで折り返す

 => DataGridViewTriState.True/False/NoSet がある

■ AcceptChanges / RejectChanges

AcceptChanges

 データセットに対して行われた変更をコミット
 Rowの状態を表す RowState は、 Unchanged になる

RejectChanges

 データセットに対して行われた変更をロールバック
 Rowの状態を表す RowState は、 Unchanged になる
 AcceptChanges と対のイメージ

■ Frozenプロパティ

 列の固定

サンプル

int COLUMN_COUNT = 6;

FileName = openFileDialog1.FileName;
string DataText = System.IO.File.ReadAllText(FileName,
    Encoding.GetEncoding("Shift_JIS"));
string[] Delimiter = {"\r\n"};
string[] StringArray = DataText.Split(
    Delimiter, StringSplitOptions.None);

int LineCounter = DataText.Length -
  DataText.Replace("\r\n", "\r").Length;
for (int i = 0; i < LineCounter; i = i + COLUMN_COUNT)
{
    string[] row = new string[COLUMN_COUNT];
    
    for (int j = 0; j < COLUMN_COUNT; j++)
    {
        row[j] = StringArray[i + j];
    }
    dataGridView1.Rows.Add(row);
}


関連記事

Windows Form ~ 目次 ~

https://blogs.yahoo.co.jp/dk521123/8054245.html

DataGridView

DataGridView ~ プロパティ編 ~
https://blogs.yahoo.co.jp/dk521123/14718079.html
DataGridView ~イベント編 ~
https://blogs.yahoo.co.jp/dk521123/23687833.html
DataGridView ~ ソート編 ~
https://blogs.yahoo.co.jp/dk521123/37914797.html
DataGridView を Label のように扱う
https://blogs.yahoo.co.jp/dk521123/29362064.html
DataGridView に ACCESS のデータを表示させる
https://blogs.yahoo.co.jp/dk521123/32859068.html
DataGridView に右クリックを適用する
https://blogs.yahoo.co.jp/dk521123/30488275.html

その他

binding ~ データバインディング
https://blogs.yahoo.co.jp/dk521123/21268762.html