In my datagridview, AllowUserToAddRow = true; When I start editing a cell, a new blank row is automatically added at the end and that is absolutely fine. But if I try to populate the whole row programmatically (e.g. say you click on a button that opens a child form, the child form when closed sends data to parent form that contains the datagridview and there you bind all data to the 开发者_JS百科datagridview row) in that case a new blank row is not added automatically. If I start editing any cell data it results in the appearance of the desired blank row. What can be the problem?
I think many of you have come across this problem earlier. Any one successfully addressed it? Please provide the soln.
Edit: Sample code attached
//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;
}
Edit:
Adding the following code snippet resolved my issue.
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);
}
}
You are going about this the wrong way - you should never need to directly add items to the DataGridView the way you are, and this is what is creating the behaviour you are seeing.
Trying instead setting foundProd as your DataGridView's DataSource (and making it a class level list) and then adding your new values to it.
Something like:
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);
}
}
}
}
I believe the bound list will automatically update - though you might want to use a BindingList instead as that certainly will.
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();
}
精彩评论