そこにDataGridView
with text があり (つまりDataGridViewTextBoxColumn
)、これらのフィールドのいずれかでテキストが変更されるたびに、更新メソッドを別の場所で呼び出す必要があります。ただし、TextBox を更新しているときにValue
、Cell
はまだ更新されていないことに気付きました。
class MyForm : Form
{
private System.Windows.Forms.DataGridView m_DataGridView;
private System.Windows.Forms.DataGridViewTextBoxColumn m_textBoxColumn;
private void m_DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs editEvent)
{
if (editEvent.Control as TextBox != null)
{
TextBox textBox = editEvent.Control as TextBox;
textBox.TextChanged -= new EventHandler(textBox_TextChanged);
textBox.TextChanged += new EventHandler(textBox_TextChanged);
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
UpdateText();
}
private void UpdateText()
{
foreach (DataGridViewRow row in m_DataGridView.Rows)
{
if (row.Cells[1].Value != null)
{
string text = row.Cells[1].Value.ToString();
System.Diagnostics.Debug.WriteLine(text);
}
}
}
}
例を挙げると、TextBox 内のテキストが現在"F"
である場合、 と入力"oo"
すると、コンソールに次のように出力されると予想されます。
"F"
"Fo"
"Foo"
代わりに、実際に書いているのは次のとおりです。
"F"
"F"
"F"
UpdateText()
TextBoxの編集中に in メソッド内からすべての TextBox の内容にアクセスする方法はありますか?