作成日: 2019/01/28 最終更新日: 2019/01/28
文書種別
使用方法
詳細
列ヘッダーのうち、特定の列の色やフォントを変えたり、テキストの配置(中央寄せ/右寄せなど)を変更する方法として、以下の3通りが考えられます。
(1) カスタムスタイルをセルに割り当てる
(2) GetCellRangeとStyleNewを使用する
(3) オーナー描画を使用してカスタムスタイルをセルに割り当てる
以下に、3通りの方法を使用して、1列目~3列目のヘッダーの外観を変更する例を記載します。
◎サンプルコード(VB)
◎サンプルコード(C#)
(1) カスタムスタイルをセルに割り当てる
(2) GetCellRangeとStyleNewを使用する
(3) オーナー描画を使用してカスタムスタイルをセルに割り当てる
以下に、3通りの方法を使用して、1列目~3列目のヘッダーの外観を変更する例を記載します。
◎サンプルコード(VB)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' (1) カスタムスタイルをセルに割り当てる
Dim newStyle1 As CellStyle = C1FlexGrid1.Styles.Add("NewStyle1")
newStyle1.BackColor = Color.Blue
newStyle1.ForeColor = Color.White
newStyle1.Font = New Font("MS 明朝", 12, FontStyle.Bold)
newStyle1.TextAlign = TextAlignEnum.RightCenter
C1FlexGrid1.SetCellStyle(0, 1, newStyle1)
' (2) GetCellRangeとStyleNewを使用する
C1FlexGrid1.GetCellRange(0, 2).StyleNew.BackColor = Color.Blue ' 新しいスタイルを作成
C1FlexGrid1.GetCellRange(0, 2).Style.ForeColor = Color.White
C1FlexGrid1.GetCellRange(0, 2).Style.Font = New Font("MS 明朝", 12, FontStyle.Bold)
C1FlexGrid1.GetCellRange(0, 2).StyleNew.TextAlign = TextAlignEnum.RightCenter ' 新しいスタイルを作成
' (3) オーナー描画を使用してカスタムスタイルをセルに割り当てる
C1FlexGrid1.DrawMode = DrawModeEnum.OwnerDraw
End Sub
Private Sub C1FlexGrid1_OwnerDrawCell(sender As Object, e As OwnerDrawCellEventArgs) Handles C1FlexGrid1.OwnerDrawCell
If e.Row = 0 And e.Col = 3 Then
Dim newStyle2 As CellStyle = C1FlexGrid1.Styles.Add("NewStyle2")
newStyle2.BackColor = Color.Blue
newStyle2.ForeColor = Color.White
newStyle2.Font = New Font("MS 明朝", 12, FontStyle.Bold)
newStyle2.TextAlign = TextAlignEnum.RightCenter
e.Style = C1FlexGrid1.Styles("NewStyle2")
End If
End Sub
' (1) カスタムスタイルをセルに割り当てる
Dim newStyle1 As CellStyle = C1FlexGrid1.Styles.Add("NewStyle1")
newStyle1.BackColor = Color.Blue
newStyle1.ForeColor = Color.White
newStyle1.Font = New Font("MS 明朝", 12, FontStyle.Bold)
newStyle1.TextAlign = TextAlignEnum.RightCenter
C1FlexGrid1.SetCellStyle(0, 1, newStyle1)
' (2) GetCellRangeとStyleNewを使用する
C1FlexGrid1.GetCellRange(0, 2).StyleNew.BackColor = Color.Blue ' 新しいスタイルを作成
C1FlexGrid1.GetCellRange(0, 2).Style.ForeColor = Color.White
C1FlexGrid1.GetCellRange(0, 2).Style.Font = New Font("MS 明朝", 12, FontStyle.Bold)
C1FlexGrid1.GetCellRange(0, 2).StyleNew.TextAlign = TextAlignEnum.RightCenter ' 新しいスタイルを作成
' (3) オーナー描画を使用してカスタムスタイルをセルに割り当てる
C1FlexGrid1.DrawMode = DrawModeEnum.OwnerDraw
End Sub
Private Sub C1FlexGrid1_OwnerDrawCell(sender As Object, e As OwnerDrawCellEventArgs) Handles C1FlexGrid1.OwnerDrawCell
If e.Row = 0 And e.Col = 3 Then
Dim newStyle2 As CellStyle = C1FlexGrid1.Styles.Add("NewStyle2")
newStyle2.BackColor = Color.Blue
newStyle2.ForeColor = Color.White
newStyle2.Font = New Font("MS 明朝", 12, FontStyle.Bold)
newStyle2.TextAlign = TextAlignEnum.RightCenter
e.Style = C1FlexGrid1.Styles("NewStyle2")
End If
End Sub
◎サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
// (1) カスタムスタイルをセルに割り当てる
CellStyle newStyle1 = c1FlexGrid1.Styles.Add("NewStyle1");
newStyle1.BackColor = Color.Blue;
newStyle1.ForeColor = Color.White;
newStyle1.Font = new Font("MS 明朝", 12, FontStyle.Bold);
newStyle1.TextAlign = TextAlignEnum.RightCenter;
c1FlexGrid1.SetCellStyle(0, 1, newStyle1);
// (2) GetCellRangeとStyleNewを使用する
c1FlexGrid1.GetCellRange(0, 2).StyleNew.BackColor = Color.Blue;
// (2) GetCellRangeとStyleNewを使用する
c1FlexGrid1.GetCellRange(0, 2).Style.ForeColor = Color.White;
c1FlexGrid1.GetCellRange(0, 2).Style.Font = new Font("MS 明朝", 12, FontStyle.Bold);
c1FlexGrid1.GetCellRange(0, 2).StyleNew.TextAlign = TextAlignEnum.RightCenter;
// (3) オーナー描画を使用してカスタムスタイルをセルに割り当てる
c1FlexGrid1.DrawMode = DrawModeEnum.OwnerDraw;
}
private void c1FlexGrid1_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
{
if (((e.Row == 0) && (e.Col == 3)))
{
CellStyle newStyle2 = c1FlexGrid1.Styles.Add("NewStyle2");
newStyle2.BackColor = Color.Blue;
newStyle2.ForeColor = Color.White;
newStyle2.Font = new Font("MS 明朝", 12, FontStyle.Bold);
newStyle2.TextAlign = TextAlignEnum.RightCenter;
e.Style = c1FlexGrid1.Styles["NewStyle2"];
}
}
{
// (1) カスタムスタイルをセルに割り当てる
CellStyle newStyle1 = c1FlexGrid1.Styles.Add("NewStyle1");
newStyle1.BackColor = Color.Blue;
newStyle1.ForeColor = Color.White;
newStyle1.Font = new Font("MS 明朝", 12, FontStyle.Bold);
newStyle1.TextAlign = TextAlignEnum.RightCenter;
c1FlexGrid1.SetCellStyle(0, 1, newStyle1);
// (2) GetCellRangeとStyleNewを使用する
c1FlexGrid1.GetCellRange(0, 2).StyleNew.BackColor = Color.Blue;
// (2) GetCellRangeとStyleNewを使用する
c1FlexGrid1.GetCellRange(0, 2).Style.ForeColor = Color.White;
c1FlexGrid1.GetCellRange(0, 2).Style.Font = new Font("MS 明朝", 12, FontStyle.Bold);
c1FlexGrid1.GetCellRange(0, 2).StyleNew.TextAlign = TextAlignEnum.RightCenter;
// (3) オーナー描画を使用してカスタムスタイルをセルに割り当てる
c1FlexGrid1.DrawMode = DrawModeEnum.OwnerDraw;
}
private void c1FlexGrid1_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
{
if (((e.Row == 0) && (e.Col == 3)))
{
CellStyle newStyle2 = c1FlexGrid1.Styles.Add("NewStyle2");
newStyle2.BackColor = Color.Blue;
newStyle2.ForeColor = Color.White;
newStyle2.Font = new Font("MS 明朝", 12, FontStyle.Bold);
newStyle2.TextAlign = TextAlignEnum.RightCenter;
e.Style = c1FlexGrid1.Styles["NewStyle2"];
}
}
関連情報
旧文書番号
83573