dポイントプレゼントキャンペーン実施中!

すぐ下に先ほど書いた質問がありますが、こちらに難点か追記してあります。下の質問は後ほど消します。(何でか、今現在消せないそうなので。)

環境 WindowsXP SP2
VisualStudio.NET 2005
C#

テストプログラムで、こんなコードを書きました。

private void button1_Click(object sender, EventArgs e)
{
int iMaxCnt;

dataGridView1.Rows.Add(1);
iMaxCnt = dataGridView1.RowCount ;

dataGridView1.EndEdit();

dataGridView1.Rows[iMaxCnt-1].Cells[0].Value = "たちつてと";
dataGridView1.Rows[iMaxCnt-1].Cells[1].Value = "なにぬめの";
dataGridView1.Rows[iMaxCnt-1].Cells[2].Value = "はひふへほ";

dataGridView1.EndEdit();

}

ボタンのクリックイベントなので、これを何度か実行すると、DataGridView の一番下の行だけにデータが表示されてしまいます。(以前のボタン押下時に記入されたデータが消えてしまい、データの変更が確定されていないように見えます。)
なぜなんでしょう?

A 回答 (2件)

よくわからず追加した


NotifyCurrentCellDirty

Updateは,なくても余裕で動くようだ(汗
    • good
    • 0

'再現した。

(VB 2008 Beta2)
'なぜかはよく俺もわからん。すまん。

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim hoge As Integer = DataGridView1.RowCount
Me.DataGridView1.Rows.Add(1)

Me.DataGridView1.CurrentCell = Me.DataGridView1(0, hoge - 1)
Me.DataGridView1.CurrentCell.Value = hoge.ToString
Me.DataGridView1.CurrentCell = Me.DataGridView1(1, hoge - 1)
Me.DataGridView1.CurrentCell.Value = (hoge + 1).ToString

Me.DataGridView1.CurrentCell = Me.DataGridView1(2, hoge - 1)
Me.DataGridView1.CurrentCell.Value = (hoge + 2).ToString

DataGridView1.NotifyCurrentCellDirty(True)
DataGridView1.Update()

End Sub
End Class

'という感じでいちいちCurrentCellにしたら出来たんだけどな・・・
'習得目的でなければ回避策として頭に入れておいてもいいかも
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!