quick background:
I notice that when a user tries to add a new row to a gridview, the row isn't bound to the data type until after the row is validated. This is problematic because I want to 'pre-populate' some of the fields based on what they enter in the first cell. If it was bound, it'd be simple as all I'd have to do is set the bound variable and the table would automatically populate...this doesn't appear feasible...
so I think I have, what I believe to be a work around...basically on a cell validation, I check to see if the row index == -1, if so then I cancel the edit and create a new CashReceivable object and add that to the bound list...the theory being that I'm esentially cancelling the edit and instead of adding the object via the gridview, I add it via the bound list...based on the code below, this works...
So what's my problem?
After this executes, the row gets added to the gridview just fine, the cell that I want to be selected is, however when I start typing to try the trustDGV.BeginEdit() (*note* I didn't include it in the code snipet below since it wasn't working, but I would assume I need to call this to put the cell into edit immediately mode) nothing happens...I am wanting it so as soon as this row gets added the second column goes directly into edit mode...What am I doing wrong? *note* when I debugged it, I noticed that the currentCell rowIndex was still set to -1, even though I am speicifically setting the current row and column....it would appear as though currentCell is never being updated properly and I might need to do it during another event, but for the life of me, I dont see which event I could do it under...
I notice that when a user tries to add a new row to a gridview, the row isn't bound to the data type until after the row is validated. This is problematic because I want to 'pre-populate' some of the fields based on what they enter in the first cell. If it was bound, it'd be simple as all I'd have to do is set the bound variable and the table would automatically populate...this doesn't appear feasible...
so I think I have, what I believe to be a work around...basically on a cell validation, I check to see if the row index == -1, if so then I cancel the edit and create a new CashReceivable object and add that to the bound list...the theory being that I'm esentially cancelling the edit and instead of adding the object via the gridview, I add it via the bound list...based on the code below, this works...
So what's my problem?
After this executes, the row gets added to the gridview just fine, the cell that I want to be selected is, however when I start typing to try the trustDGV.BeginEdit() (*note* I didn't include it in the code snipet below since it wasn't working, but I would assume I need to call this to put the cell into edit immediately mode) nothing happens...I am wanting it so as soon as this row gets added the second column goes directly into edit mode...What am I doing wrong? *note* when I debugged it, I noticed that the currentCell rowIndex was still set to -1, even though I am speicifically setting the current row and column....it would appear as though currentCell is never being updated properly and I might need to do it during another event, but for the life of me, I dont see which event I could do it under...
public ReceiptSplitWizard(String receiptNumber)
{
InitializeComponent();
trustReceivables = new BindingList<
CashReceivable
>();
trustDGV.DataSource = trustReceivables;
trustDGV.CellValidated += new CellValidatedEventHandler(trustDGV_CellValidated);
trustDGV.RowsChanged += new GridViewCollectionChangedEventHandler(trustDGV_RowsChanged);
... foreach loop here to add pre-existing receivables to the binding list to pre-populate it ...
}
void trustDGV_CellValidated(object sender, CellValidatedEventArgs e)
{
if (e.RowIndex == -1)
{
if (e.Column.UniqueName.Equals("trustTargetInvoiceNumberDGVTBC"))
{
CashReceivable receivable = CashReceivable.Clone(originalReceivable);
receivable.TXSet = null;
receivable.BillInvoiceNo = (String)e.Value;
receivable.CheckAmount = null;
receivable.BusinessFunction = GetBusinessFunction(receivable.BillInvoiceNo);
trustReceivables.Add(receivable);
trustDGV.MasterView.TableAddNewRow.CancelAddNewRow();
trustDGV.CurrentRow = trustDGV.Rows[trustDGV.Rows.Count - 1];
trustDGV.CurrentColumn = trustDGV.Columns[1];
}
}
}
void trustDGV_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
{
GridViewDataRowInfo row = (GridViewDataRowInfo)e.NewItems[0];
CashReceivable receivable = (CashReceivable)row.DataBoundItem;
// if it's been distributed, then gray it out and set it readonly
if (receivable.TXSet != null)
{
foreach (GridViewCellInfo cell in e.GridViewTemplate.Rows[e.NewStartingIndex].Cells)
{
cell.Style.Font = new Font("Segoe UI", 8.25F, FontStyle.Italic);
cell.Style.ForeColor = Color.SlateGray;
cell.ReadOnly = true;
}
}
}