正在尋找替代方法,我目前如何做到這一點,只是想讓你知道一些我知道的方法。這些都將在使用*(新行)或使用BindingNavigator時設置默認值。
private void CityBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
// Simply set the default value to the next viable ID, will automatically set the value when needed.
locationDataSet.CITY.CITY_IDColumn.DefaultValue = CityTableAdapter.GetNewID(); // ++maxID;
}
或者
private void CityDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
// Gets called when datagridview's */NewRow is entered.
e.Row.Cells[0].Value = CityTableAdapter.GetNewID(); // ++maxID;
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
// BindingNavigator button's AddNewItem automatic tie-in to bindingSource's AddNew() is removed
// This sets the current cell to the NewRow's cell to trigger DefaultValuesNeeded event
CityDataGridView.CurrentCell = CityDataGridView.Rows[CityDataGridView.NewRowIndex].Cells[1];
}
我真的很希望用這個...
private void CityBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
// Cast your strongly-typed bindingsource List to a DataView and add a new DataRowView
DataRowView rowView = ((DataView)CityBindingSource.List).AddNew();
var newRow = (LocationDTSDataSet.CITYRow)rowView.Row;
newRow.CITY_ID = CityTableAdapter.GetNewID();
newRow.CMMT_TXT = "Whatever defaults I want";
e.NewObject = rowView;
}
但使用它導致BindingNavigator或基於bindingSource.AddNew(任何代碼)時加入只是一排空白。希望有人找到更好的方法,但這對我來說迄今爲止。
嗨,斯圖亞特!感謝您的迴應。由於文檔正確地聲明「由於無法將新的DataRowView添加到列表,因此綁定到DataView或DataTable時無法設置NewObject屬性。」這是棘手的部分,讓我頭痛(我使用數據表與數據集)! – 2010-10-17 20:19:47
看起來好多了!我會試試這個,讓你知道!謝謝 – 2010-10-18 19:38:56