DataGridView에 바인딩 소스가있는 경우 DefaultValuesNeeeded
이벤트 처리기에서 EndCurrentEdit()
을 호출하여 기본값을 사용하여 새 행을 즉시 커밋 할 수 있습니다. 당신이 바인딩 소스가없는 경우
{
dt = new DataTable();
dt.Columns.Add("Cat");
dt.Columns.Add("Dog");
dataGridView1.AllowUserToAddRows = true;
dataGridView1.DefaultValuesNeeded += dataGridView1_DefaultValuesNeeded;
dataGridView1.DataSource = dt;
}
void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
var dgv = sender as DataGridView;
if(dgv == null)
return;
e.Row.Cells["Cat"].Value = "Meow";
e.Row.Cells["Dog"].Value = "Woof";
// This line will commit the new line to the binding source
dgv.BindingContext[dgv.DataSource].EndCurrentEdit();
}
, 우리는 그것이 작동하지 않기 때문에 DefaultValuesNeeded
이벤트를 사용하지 않을 수 있습니다. 그러나 CellEnter
이벤트를 캡처하여 시뮬레이션 할 수 있습니다.
{
dataGridView1.Columns.Add("Cat", "Cat");
dataGridView1.Columns.Add("Dog", "Dog");
dataGridView1.AllowUserToAddRows = true;
dataGridView1.CellEnter += dataGridView1_CellEnter;
}
void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
var dgv = sender as DataGridView;
if (dgv == null)
return;
var row = dgv.Rows[e.RowIndex];
if (row.IsNewRow)
{
// Set your default values here
row.Cells["Cat"].Value = "Meow";
row.Cells["Dog"].Value = "Woof";
// Force the DGV to add the new row by marking it dirty
dgv.NotifyCurrentCellDirty(true);
}
}
DataGridView에 바인딩 소스가 있습니까? – Moop