正在寻找替代方法,我目前如何做到这一点,只是想让你知道一些我知道的方法。这些都将在使用*(新行)或使用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