在我的datagridview中,AllowUserToAddRow = true;当我开始编辑单元格时,最后会自动添加一个新的空白行,这绝对没问题。但是,如果我尝试以编程方式填充整行(例如,假设您单击打开子表单的按钮,则子表单在关闭时将数据发送到包含datagridview的父表单,并将所有数据绑定到datagridview行)在这种情况下,不会自动添加新的空白行。如果我开始编辑任何单元格数据,则会导致所需空行的外观。可能是什么问题?
编辑:附加示例代码
//Uses Linq-to-Entities
IQueryable<Product> productQuery = from prod in sampleTaskContext.Product
where prod.ProductId == enteredProdId
select prod;
List<Product> foundProd = productQuery.ToList();
if (foundProd.Count <= 0)
{
ProductsForm prodForm = new ProductsForm();
prodForm.ShowInTaskbar = false;
if (prodForm.ShowDialog() == DialogResult.OK)
{
Product returnedProd = prodForm.fnFormatProductDataForOrder();
foundProd.Add(returnedProd);
}
}
if (foundProd.Count > 0)
{
int selectedRowIndex = this.dgvOrderEntryDetails.SelectedCells[0].RowIndex;
dgvOrderEntryDetails.Rows[selectedRowIndex].Cells["colProdId"].Value = foundProd[0].ProductId;
dgvOrderEntryDetails.Rows[selectedRowIndex].Cells["colDesc"].Value = foundProd[0].ItemDescription.ToString();
dgvOrderEntryDetails.Rows[selectedRowIndex].Cells["colQty"].Value = 1;
dgvOrderEntryDetails.Rows[selectedRowIndex].Cells["colListPrice"].Value = foundProd[0].SalesPrice;
dgvOrderEntryDetails.Rows[selectedRowIndex].Cells["colDisc"].Value = 0;
dgvOrderEntryDetails.Rows[selectedRowIndex].Cells["colPrice"].Value = foundProd[0].SalesPrice;
dgvOrderEntryDetails.Rows[selectedRowIndex].Cells["colTotal"].Value = foundProd[0].SalesPrice;
编辑:
答案 0 :(得分:1)
添加以下代码段解决了我的问题。
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
{
base.OnCellBeginEdit(e);
if (e.RowIndex == this.NewRowIndex)
{
if (this[e.ColumnIndex, e.RowIndex].Value == null)
return;
string value = this[e.ColumnIndex, e.RowIndex].Value.ToString();
SendKeys.Send("{BackSpace}");
SendKeys.Send(value);
}
}
答案 1 :(得分:0)
你会以错误的方式解决这个问题 - 你永远不需要按照自己的方式直接向DataGridView添加项目,这就是创建你所看到的行为的原因。
尝试将foundProd设置为DataGridView的DataSource(并使其成为类级别列表),然后将新值添加到其中。
类似的东西:
public partial class Form1 : Form
{
private List<Product> foundProd_;
public Form1()
{
InitializeComponent();
foundProd_ = new BindingList<FoundProd>();
dataGridView1.DataSource = foundProd;
}
// This method is where you update the datasource from the linq query
private void SomeMethod()
{
IQueryable<Product> productQuery = from prod in sampleTaskContext.Product
where prod.ProductId == enteredProdId
select prod;
List<Product> foundProd = productQuery.ToList();
if (foundProd.Count <= 0)
{
ProductsForm prodForm = new ProductsForm();
prodForm.ShowInTaskbar = false;
if (prodForm.ShowDialog() == DialogResult.OK)
{
Product returnedProd = prodForm.fnFormatProductDataForOrder();
foundProd_.Add(returnedProd);
}
}
}
}
我相信绑定列表会自动更新 - 尽管您可能希望使用BindingList,但肯定会这样。
答案 2 :(得分:0)
if (dgorderdtl.CurrentRow.Index + 1 == dgorderdtl.Rows.Count)
{
this.dgorderdtl.CurrentRow.Cells[dgorderdtl.CurrentCell.ColumnIndex - 1].Value = oBrowseLookUp.SelectedIDVal;
this.dgorderdtl.CurrentRow.Cells[dgorderdtl.CurrentCell.ColumnIndex].Value = oBrowseLookUp.SelectedDescVal;
dgorderdtl.Rows.Add();
dgorderdtl_CellValueChanged(null, null);
this.dgorderdtl.RefreshEdit();
}