■ 概要
Labelは非常に扱いやすいが、以下の欠点がある * コピーできない * レイアウトが綺麗に配置するのが、めんどい そこで、DataGridView を Label のように扱うのをメモ。 (別にたいしたことじゃないけど、今後、同じようなことをやりそうなので)
■ サンプル
DataGridViewのプロパティ
BackgroundColor : Control=> 背景色をコントロール側に併せるBorderStyle : None
=> グリッド線を非表示ColumnHeadersVisible : False
=> ヘッダー行を非表示GridColor : Control
=> グリッド線をコントロール側に併せるReadOnly : True
=> 読み取り専用にするRowDefaultCellStyle
* RowsDefaultCellStyle.BackColr : Control
=>
* RowsDefaultCellStyle.SelectionBackColor : DarkGray
=> 選択した時のフォーカス色(オプション)
RowsHeadersVisible : False => ヘッダー列を非表示
Form1.cs
public partial class Form1 : Form
{
private List<DataItemContainer> dataItems;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.dataItems = new List<DataItemContainer>()
{
new DataItemContainer() {Item = "Id", Value = "X-001", },
new DataItemContainer() {Item = "Name", Value = "Sam", },
new DataItemContainer() {Item = "Age", Value = "32", },
};
this.dataGridView1.DataSource = this.dataItems;
}
private void Form1_Shown(object sender, EventArgs e)
{
// 起動時にセルが選択されないようにする
this.dataGridView1.CurrentCell = null;
}
}
public class DataItemContainer
{
public string Item { get; set; }
public string Value { get; set; }
}
■ 注意
* 起動時にセルが選択されないようにするには、Shownイベントで「CurrentCell = null」する
参考文献
起動時にセルが選択されないようにするhttp://www.atmarkit.co.jp/fdotnet/dotnettips/512dgvcurrent/dgvcurrent.html
関連記事
DataGridView ~ プロパティ ~http://blogs.yahoo.co.jp/dk521123/14718079.html
DataGridView ~ DataGridView あれこれ [1] ~
http://blogs.yahoo.co.jp/dk521123/20904670.html