ユーザーがレコードを直接追加できるようにするDataGridViewがあります。これは、DGVの下のリンクをクリックすることによって実行されます。この時点で、新しい行がプログラムで追加され、最初に表示されるセルはComboBoxCellタイプです。私がこれをどのように行っているかについてのコードの抜粋は次のとおりです。
// Add a new row to DGV
DataGridView dgv = this.dgvInformation;
DataGridViewRow newRow = new DataGridViewRow();
dgv.Rows.Add(newRow);
// Create cells and add to row
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell();
newRow.Cells["InfoType"] = cellInfoType;
// Create DataSource based off LINQ query here
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>();
// List is populated here
// Assign DataSource to combo cell
cellInfoType.DataSource = comboDataSource;
cellInfoType.ValueMember = "AccInfoTypeID";
cellInfoType.DisplayMember = "InfoType";
// Scroll new row into view and begin editing
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1];
dgv.BeginEdit(true);
データソースには、IDが-1の値がいくつか含まれています。これらの値は、ユーザーが選択できないカテゴリであり、他のすべての値には有効なデータベースIDがあります。これはすべて正常に機能しますが、ユーザーが-1行を選択した場合、コンボセルはセルに値を格納するだけで、ドロップダウンをアクティブにできないため、コンボセルを編集し続けることができません。_CellValueChangedイベントに次のコードを追加しましたが、効果がありません。
private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex];
// If type of cell is ComboBox then
if (cell.GetType() == typeof(DataGridViewComboBoxCell))
{
DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell;
if ((int)cellInfoType.Value == -1)
{
MessageBox.Show("Going back into edit mode...");
dgv.CurrentCell = cell;
dgv.BeginEdit(true);
}
else
{
MessageBox.Show(cellInfoType.Value.ToString());
}
}
}
別のセルに移動した後、ここに「編集モードに戻ります...」というメッセージが表示されますが、そのメッセージは表示されません。なぜ編集モードに戻らないのか、選択するとすぐに値がロックされないようにする方法はありますか?
どうもありがとう!