Dynamically change the dataGridView's column value based on user input

andrewmanuja

Well-known member
Joined
May 30, 2019
Messages
75
Programming Experience
Beginner
Hi All,

I got a dataGridView which got the following fields,
orderID, drugID, requestquantity and balancequantity

MyDataGridView looks like below;

541


The above said data is loaded from a SQL DB table.
What I want to perform is, whenever a user enter a value in to the "Issue Quantity" column, the dataGridView's "Balance Quantity" should get automatically adjusted.

For an example, if I enter 20 against Drug ID "10", I should be able to see the respective"Issue Quantity" as "40".

Similarly , I want to see the change for the Balance quantity for all the other products in the DataGridView too.

I tried something like below;

C#:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        row.Cells[dataGridView1.Columns["balancequantity"].Index].Value = (Convert.ToInt32(row.Cells[dataGridView1.Columns["balancequantity"].Index].Value) - Convert.ToInt32(row.Cells[dataGridView1.Columns["issuequantity"].Index].Value));
    }
}

However, this does not give me the desired output. For an example, If I keep on changing the "Issue Quantity" value, and click somewhere else, it will keep on dropping the "Balance Quantity" value.

Thank you in advance.

Kind regards,

Andrew
 
Last edited by a moderator:
Is grid bound to a data source, e.g. a DataTable? If the data is coming from a database then it really ought to be. Whether it is bound and what it is bound to will make a difference in how best you would perform the calculation. Also, if it is bound, are all the columns bound or only some of them? It seems to me that perhaps Issue Quantity and Request Quantity might not be, even if the rest were.
 
By the way, your code as it is doesn't really make sense. Even if the calculation did as you wanted, there's no reason to update every row in the grid when the user makes a change to a single row. The CellEndEdit event, like every other cell-based event, tells you what column and row the cell was in, so you can ignore any cells in irrelevant columns and only update the relevant row.
 
Hi,
Thanks for your update.

Yes the data is bound to a datatable. The intention is allowing user to enter the "Issue Quantity" until it reaches the "Balance Quantity". For example, at the moment the "Balance Quantity" is 50 and 40 respectively. Assume, the user issue 20 each for "Drud ID" 10 and 11 and Save the data. Once he open the window again, the "Balance Quantity" for "Drug ID" 10 and 11 will be 30 and 20.

Thus, the user may issue drugs for selected "Drug ID"s for a individual "Order ID" or for all.

That is why, I need to consider individual data record and accordingly change the "Balance Quantity" value.

Hope this is clear to you.

Look forward to hearing from you.

Thank you.

Regards,

Andrew
 
That makes sense if the user enters 20 for the rows for Drug ID 10 and 11. But your current code above goes through all the rows with your foreach (DataGridViewRow row in dataGridView1.Rows) regardless of which row was edited.
 
As an aside, like I told you in the other thread, it is typically a bad idea to be playing with values in the View (in this case the DataGridView). You should really be playing with values from your Model (your DataTable to which you are bound to). Update the data in your Model, and let the view refresh.

Yes, back in the 80's and 90's computers didn't have as much memory as they do now and so the typical approach was that data got loaded into the view, and the view played both the role of presenting UI as well as acting as an in memory data store. Those days have gone by and people have learned it is better to keep the View and Model separate from each other.
 
As an aside, like I told you in the other thread, it is typically a bad idea to be playing with values in the View (in this case the DataGridView). You should really be playing with values from your Model (your DataTable to which you are bound to). Update the data in your Model, and let the view refresh.

Yes, back in the 80's and 90's computers didn't have as much memory as they do now and so the typical approach was that data got loaded into the view, and the view played both the role of presenting UI as well as acting as an in memory data store. Those days have gone by and people have learned it is better to keep the View and Model separate from each other.

Hi,

Thank you very much for your reply.

As you said, instead of calculating the balance quantity each time a user enter a value, I simply pass the entire set of values to the database table irrespective of how many records the user have updated.

Also, check the "Balance Quantity" value when saving the data to the DB.

If need further clarifications, I'll post them on the forum.

Kind regards,

Andrew
 
Back
Top Bottom