Devexpress gridview cell value not appear immediately - c#

I have created an event handler to display data based on user selected value.
private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
DataRow dataRow = gridView1.GetDataRow(e.RowHandle);
//Did some query to get firstDataRow
dataRow["UOM"] = firstDataRow["SalesUOM"].ToString();
dataRow["Description"] = firstDataRow["Description"].ToString();
dataRow["Desc2"] = firstDataRow["Desc2"].ToString();
dataRow["FurtherDescription"] = firstDataRow["FurtherDescription"].ToString();
dataRow["Qty"] = 1;
dataRow["UnitPrice"] = AutoCount.Converter.ToDecimal(firstDataRow["Price"]);
dataRow["UnitCost"] = AutoCount.Converter.ToDecimal(firstDataRow["Cost"]);
}
The problem is the value does not appear immediately, but only appear after the user click outside the row. If the value does not appear but save operation is carried out, the value will be save in database, proving the data is there but does not displayed out in the interface.

Related

How to get a ComboBox in a DataGridViewCell to drop down after a single click?

I have a DataGridView which has it's first column's style set to a ComboBox rather than the default TextBox. Since the number of rows in the DataGridView is not fixed on startup I cannot load in data to the ComboBoxes for each row when a new row is added. So I tried loading on the event of a user adding a row to the DataGridView:
public void myDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
// Identifiers used are:
var myTableAdapter = new databaseTableAdapters.myTableTableAdapter();
var myDataTable = myTableAdapter.GetData();
int rowIndex = myDataGridView.CurrentcellAddress.Y;
var comboBoxCell = (DataGridViewComboBoxCell)myDataGridView.Rows[rowIndex].Cells[0];
string itemToAdd;
// Load in the data from the data table
foreach (System.Data.DataRow row in myDataTable.Rows)
{
// Get the current item to be added
itemToAdd = row[0].ToString();
// Make sure there are no duplicates
if (!comboBoxCell.Items.Contains(itemToAdd))
{
comboBoxCell.Items.Add(itemToAdd)
}
}
}
but this only will allow the user to see the drop down options after a second click. I would like to be able to have the user only click on the combo box once and see the options rather than the less intuitive double click. How can this be done?
The cell must gain focus for the drop down to occur, so the double click is actually a single click to gain focus on that cell and the second click is what causes the drop down to occur. So to see how to change focus following this link. I was able to modify the code with a single line of code
public void myDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
// Identifiers used are:
var myTableAdapter = new databaseTableAdapters.myTableTableAdapter();
var myDataTable = myTableAdapter.GetData();
int rowIndex = myDataGridView.CurrentcellAddress.Y;
var comboBoxCell = (DataGridViewComboBoxCell)myDataGridView.Rows[rowIndex].Cells[0];
string itemToAdd;
// Load in the data from the data table
foreach (System.Data.DataRow row in myDataTable.Rows)
{
// Get the current item to be added
itemToAdd = row[0].ToString();
// Make sure there are no duplicates
if (!comboBoxCell.Items.Contains(itemToAdd))
{
comboBoxCell.Items.Add(itemToAdd)
}
}
// Send the focus to the next combo box (removes need for a double click)
myDataGridView.CurrentCell = myDataGridView.Rows[rowIndex + 1].Cells[0]; // <--- HERE
}

Datagridview winforms cells doesnt get updated unless focus back on the grid

I am working with winforms c# datagridview and ran into a bug that I m not sure how to resolve. I have a datagridview in which the datasource is a datatable. The datagridview contains editable text fields and cellvaluechanged event as well as button column which I respond to thru a cellclick event. Currently when I change the value in a cell , hit the enter key to leave the just updated cell and click the button. The cellclick event which acts as the button click runs a task (TPL) in which the continuewith of the tasks updates values in the datagridview and that works because all the values that needed to be changed gets changed. The issue is that when I change a text value and don't hit the enter key and just immediately click the button on the row. the ui doesn't get updated intill I click on the cells to see the new values. I have looked this up and found that if I set the
datagridview.Currentcell = datagridview.Rows[e.RowIndex].Cells[4]
to the cells that I want to see the new values to. The new values appear. I don't want run for each cell I was wondering if there is a better way in handling this issue?
This is the code:
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
//Button on grid
if (e.ColumnIndex == 17)
{
string val1 = dgv.Rows[e.RowIndex].Cells[3].Value.ToString();
string val2 = dgv.Rows[e.RowIndex].Cells[5].Value.ToString();
Task.Factory.StartNew<Dictionary<string, Response>>(() => GenerateData(val1, val2)).ContinueWith(ant => {
int rowId = Convert.ToInt32(this.dgv.Rows[e.RowIndex].Cells[1].Value);
SetResponse(rowId, this.DataSet.DataTable, ant.Result);
// i have tried the code below and it fixes the issue but don't want to go this route
//for (int i = 5; i < 12; i++) dgv.CurrentCell = dgv.Rows[e.RowIndex].Cells[i];
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
}
private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5)
{
string val1 = this.dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Trim();
if (!string.IsNullOrEmpty(val1))
{
//dgv.BeginEdit(false);
this.dgv.Rows[e.RowIndex].Cells[6].Value = DBNull.Value;
//dgv.EndEdit(DataGridViewDataErrorContexts.Commit);
}
}
}
By adding these two lines of code resolved the issue
this.dgv.EndEdit();
this.dgv.CurrentCell = null;

Display Member is not display at the first time event fired in RepositoryLookupEdit in Winforms Gridview Devexpress?

I used RepositoryLookupEdit in my 1st Column of Gridview. Using EditValueChanged Event remaining columns fill automatically, till now every thing is working fine. Then I call one Function inside RepositoryLookupEdit, After that very first time this EditValueChanged event fired my Display Member is not shown, it simply shows Null value that i given. Next time if I reselect same column it working fine. What was wrong here ?
private void repositoryItemGridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
GridLookUpEdit LookupEdit = sender as GridLookUpEdit;
DataRowView SelectedDataRow = (DataRowView)LookupEdit.GetSelectedDataRow();
gridView1.SetFocusedRowCellValue("Description", SelectedDataRow["ProductDescription"]);
gridView1.SetFocusedRowCellValue("UoM", SelectedDataRow["UnitofMeasure"]);
gridView1.SetFocusedRowCellValue("Quantity", SelectedDataRow["DefaultQuantity"]); //UnitPrice
gridView1.SetFocusedRowCellValue("UnitPrice", SelectedDataRow["MRPPrice"]);
gridView1.SetFocusedRowCellValue("DiscountPercentage", SelectedDataRow["Discount"]);
gridView1.SetFocusedRowCellValue("DiscountAmount", SelectedDataRow["DiscountAmount"]);
gridView1.SetFocusedRowCellValue("TaxInPercentage", SelectedDataRow["Taxid1"]);
Taxamountcalc(); // function to calculate taxamount
rowcount = gridView1.RowCount;
}
In this above code if i remove or comment "Taxamountcalc()" then my display member display properly if i enables this function then it show "NullText" only.
private void Taxamountcalc()
{
if (barCheckItem1.Checked)
{
TaxAmount.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
TaxAmount.UnboundExpression = "Round([UnitTotal] * ([TaxInPercentage] / 100), 2)";
}
else if (!(barCheckItem1.Checked))
{
TaxAmount.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
TaxAmount.UnboundExpression = "Round(([UnitPrice] * ([TaxInPercentage] / 100)) * [Quantity], 2)";
}
}
Don't call any function from RepositoryLookupdit EditValueChanged Event. If we call any function from EditValueChanged Event, for the First time selecting any values from lookupedit display Member is not displays it simply shows nulltext.
private void repositoryItemGridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
GridLookUpEdit LookupEdit = sender as GridLookUpEdit;
DataRowView SelectedDataRow = (DataRowView)LookupEdit.GetSelectedDataRow();
gridView1.SetFocusedRowCellValue("Description", SelectedDataRow["ProductDescription"]);
gridView1.SetFocusedRowCellValue("UoM", SelectedDataRow["UnitofMeasure"]);
gridView1.SetFocusedRowCellValue("Quantity", SelectedDataRow["DefaultQuantity"]); //UnitPrice
gridView1.SetFocusedRowCellValue("UnitPrice", SelectedDataRow["MRPPrice"]);
gridView1.SetFocusedRowCellValue("DiscountPercentage", SelectedDataRow["Discount"]);
gridView1.SetFocusedRowCellValue("DiscountAmount", SelectedDataRow["DiscountAmount"]);
gridView1.SetFocusedRowCellValue("TaxInPercentage", SelectedDataRow["Taxid1"]);
// Taxamountcalc();
// rowcount = gridView1.RowCount;
}
Now it is working properly

All rows selected on clicked

I'm building a application in C# Winforms and using a Datagridview.
My problem is that when I click on a row, all rows are selected (without intention) and the application crashes (as the values inserted from the grid view selected row into labels are not intact (too many values...).
The correct behavior I'm looking for is the selection of on row only.
I thought the problem might be in the selectionMode (I used the value "RowHeaderSelect"), but I changed it, and the problem persisted, so it isn't it.
Do you have an idea what might be the problem ??
Not relying on much code, really, but here it is:
private void dgvCustomersList_MouseClick(object sender, MouseEventArgs e)
{
{
customerFunctions ChoosenRow = new customerFunctions(); //empty
DataGridViewRow dr = dgvCustomersList.SelectedRows[0];
ChoosenRow.CfirstName = dr.Cells[1].Value.ToString();
ChoosenRow.ClastName = dr.Cells[2].Value.ToString();
ChoosenRow.Caddress = dr.Cells[3].Value.ToString();
ChoosenRow.CcreditNumber = int.Parse(dr.Cells[7].Value.ToString());
ChoosenRow.CpersonalID = int.Parse(dr.Cells[5].Value.ToString());
}
}

DataGridView selecting a specific row and retrieving all column values to an object array

I want to perform operation that On a button click event , Grid Currentrow entire data is passed to an object array
I have tried to search through following links :
DataGridView selecting a specific row and retrieving its values
Getting data from selected datagridview row and which event?
But they are talking about particular cell value
i tried to perform with code
DataRowView currentDataRowView = (DataRowView)grdGLSearch.CurrentRow.DataBoundItem;
DataRow row1 = currentDataRowView.Row;
But currentDataRowView is retrieving null
one of My Senior succesfully created a generic property GetSelectedRow()
it works like this :
var object =grdGLSearch.GetSelectedRow<T>();
and it has definition
public T GetSelectedRow<T>()
{
if (this.CurrentRowIndex == -1)
{
return default(T);
}
return (base.DataSource as BindingList<T>)[this.CurrentRowIndex];
}
But it is binded to only one Main grid , i also want to use this property to another Grids
I dont want data of a particular column , i want entire row data .. and dont want any iteration to be perform ...
Is there any single liner operation for this ?
Please suggest if I am missing any links
Since you are only showing 2 lines of code, I am really not sure exactly what is going on. First off, I get you want the entire row when you click some button. However, you never state how the current row is being selected.
In order to get the full selected row, you must have a selected cell. The MSDN documentation says this on the page for DataGridView.SelectedRow. So I am assuming that the user will click a cell, and you want the entire row. I would create a variable that will hold your selected row. Then when the user clicks the cell, automatically select the row and save it. Then when the button is clicked, just retrieve the the already saved row.
private DataGridViewRow selectedRow { get; set; }
Then have the event for when the user clicks a cell
private void grdGLSearch_CellClick(object sender, DataGridViewCellEventArgs e)
{
selectedRow = grdGLSearch.Rows[e.RowIndex];
}
Finally, the button click event
private void SubmitBtn_ItemClick(object sender, ItemClickEventArgs e)
{
// to target the specific data
var cellVal1 = selectedRow.Cells["SpecificCell1"].Value;
var cellVal2 = selectedRow.Cells["SpecificCell2"].Value;
}

Categories