セルの値を一括して初期化するには?

文書番号 : 26095     文書種別 : 使用方法     最終更新日 : 2008/07/29
文書を印刷する
対象製品
MultiRow for Windows Forms 5.0J
詳細
セルの値を初期化するには、セルの値(Cell.Value プロパティ)に null (Visual Basic では Nothing)を設定します。セルの値は HeaderCell や ButtonCell でキャプションにも使用されており、設計されるレイアウトによって期待される動作が異なることから、これを一括して初期化するメソッドは提供されていません。一括して初期化を行うには、次のようにコーディングする必要があります。

1.Cell.ReadOnly=Falseのセルだけを初期化する

[Visual Basic]
Imports GrapeCity.Win.MultiRow

For Each row As Row In Me.GcMultiRow1.Rows
  For Each cell As Cell In row.Cells
    If cell.ReadOnly = False Then
      cell.Value = Nothing
    End If
  Next
Next


[C#]
using GrapeCity.Win.MultiRow;

foreach (Row row in this.gcMultiRow1.Rows)
{
  foreach (Cell cell in row.Cells)
  {
    if (cell.ReadOnly == false)
      cell.Value = null;
  }
}


または

[Visual Basic]
Imports GrapeCity.Win.MultiRow

For r As Integer = 0 To Me.GcMultiRow1.RowCount - 1
  For c As Integer = 0 To Me.GcMultiRow1.Template.Row.Cells.Count - 1
    If Me.GcMultiRow1.Rows(r).Cells(c).ReadOnly = False Then
      Me.GcMultiRow1.SetValue(r, c, Nothing)
    End If
  Next
Next


[C#]
using GrapeCity.Win.MultiRow;

for (int r = 0; r < this.gcMultiRow1.RowCount; r++)
{
  for (int c = 0; c < this.gcMultiRow1.Template.Row.Cells.Count; c++)
  {
    if (this.gcMultiRow1.Rows[r].Cells[c].ReadOnly == false)
      this.gcMultiRow1.SetValue(r, c, null);
  }
}


2.行数を初期化する

[Visual Basic]
Imports GrapeCity.Win.MultiRow

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Me.GcMultiRow1.AllowUserToAddRows = False
  Me.GcMultiRow1.RowCount = 10
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim count As Integer = Me.GcMultiRow1.RowCount
  Dim cellPos As CellPosition = Me.GcMultiRow1.CurrentCellPosition
  Me.GcMultiRow1.SuspendLayout()
  ' すべてのセルを初期化する
  Me.GcMultiRow1.RowCount = 0
  ' グリッドの行数を復帰する
  Me.GcMultiRow1.RowCount = count
  ' セルの位置を復帰する
  Me.GcMultiRow1.CurrentCellPosition = cellPos
  Me.GcMultiRow1.ResumeLayout()
End Sub


[C#]
using GrapeCity.Win.MultiRow;

private void Form1_Load(object sender, EventArgs e)
{
  this.gcMultiRow1.AllowUserToAddRows = false;
  this.gcMultiRow1.RowCount = 10;
}

private void button1_Click(object sender, EventArgs e)
{
  int count = this.gcMultiRow1.RowCount;
  CellPosition cellPos = this.gcMultiRow1.CurrentCellPosition;
  this.gcMultiRow1.SuspendLayout();
  // すべてのセルを初期化する
  this.gcMultiRow1.RowCount = 0;
  // グリッドの行数を復帰する
  this.gcMultiRow1.RowCount = count;
  // セルの位置を復帰する
  this.gcMultiRow1.CurrentCellPosition = cellPos;
  this.gcMultiRow1.ResumeLayout();
}


3.Row.SetValues メソッドを使用する
注意:Cell.ReadOnly の設定は無視されます。

[Visual Basic]
' Null(Nothing)の配列を作成する
Dim initValues() As Object = New Object(Me.GcMultiRow1.Template.Row.Cells.Count) {}
For i As Integer = 0 To Me.GcMultiRow1.RowCount - 1
  Me.GcMultiRow1.Rows(i).SetValues(initValues)
Next


[C#]
// nullの配列を作成する
object[] initValues = new object[this.gcMultiRow1.Template.Row.Cells.Count];
for (int i = 0; i < this.gcMultiRow1.RowCount; i++)
{
  this.gcMultiRow1.Rows[i].SetValues(initValues);
}


4.ユーザーの操作をシミュレートする
この方法の場合、ユーザーと同じように Cell.ReadOnly プロパティの設定の
影響を受けます。

[Visual Basic]
Imports GrapeCity.Win.MultiRow

Dim cellPos As CellPosition = Me.GcMultiRow1.CurrentCellPosition
Me.GcMultiRow1.SuspendLayout()
SelectionActions.SelectAll.Execute(Me.GcMultiRow1)
EditingActions.Clear.Execute(Me.GcMultiRow1)
SelectionActions.MoveToFirstCell.Execute(Me.GcMultiRow1)
Me.GcMultiRow1.CurrentCellPosition = cellPos
Me.GcMultiRow1.ResumeLayout()

[C#]
using GrapeCity.Win.MultiRow;

CellPosition cellPos = this.gcMultiRow1.CurrentCellPosition;
this.gcMultiRow1.SuspendLayout();
SelectionActions.SelectAll.Execute(this.gcMultiRow1);
EditingActions.Clear.Execute(this.gcMultiRow1);
SelectionActions.MoveToFirstCell.Execute(this.gcMultiRow1);
this.gcMultiRow1.CurrentCellPosition = cellPos;
this.gcMultiRow1.ResumeLayout();

この文書は、以前は次のFAQ IDで公開されていました : 11386