How to update the value in cells in a grid - c#

i have a Grid and i have 4 rows and I need to update the 4th row based on the inputs in the 1st,2nd and 3rd row values .
Example :
1st when you enter 1 the total in the 4th row should be 1
when you enter 1 in 1st row and 2 in 2nd row the total shoud be 3 .
Should we use java script as these rows are in a grid when we hit a pop up on the page .
let me know if I am missing anything

This question has been already discussed in the IG forum at: http://www.infragistics.com/community/forums/t/77638.aspx
Here's what I wrote there:
You could handle the AfterCellUpdateHandler. It will fire after a
cell’s value has been changed so in it you could look through the cell
values and calculate the sum. After that you can change the last
cell’s value. For example:
function UltraWebGrid1_AfterCellUpdateHandler(gridName, cellId){
var cell = igtbl_getCellById(cellId);
var totalCell = cell.Row.getCellFromKey("Total");
var sum = 0;
for (var i = 1; i < cell.Row.cells.length-1; i++) {
sum += cell.Row.getCell(i).getValue();
}
cell.Row.getCellFromKey("Total").setValue(sum);
}
Please refer to the attached sample. In it if you change the value in
any of the cells the value of the related cell from the column “Total”
will be calculated based on the new values.
Let me know if you have any questions.

You can do something like
dataGridView1.Rows[0].Cells[0].Value = Convert.ToInt32(dataGridView1.Rows[1].Cells[0].Value) + Convert.ToInt32(dataGridView1.Rows[2].Cells[0].Value) ;
In the [ ], you will have to enter desired index of row or column.

You can use Unbound field
see this example Unbound field
here you can see unbound field of subtotal,SalesTax and Total and code done at cs side in InitializeRecord event.

Related

Index out of range in C# getting data from a datagridview

I am trying to get data out of 2 cells on the last line of DataGridView in C#. These lines of code give me Index was out of range. Must be non-negative and less than the size of the collection.
intNumberOfRows = dgvWellsFargo.RowCount;
txtNoOfTransactions.Text = dgvWellsFargo.Rows[intNumberOfRows].Cells[1].Value.ToString();
txtTotal.Text = String.Format("{0:c2}", dgvWellsFargo.Rows[intNumberOfRows].Cells[2].Value.ToString());
As you can see I am trying to use the RowCount for my index which I assume is what I need to use. Is there another parameter that I should be using?
I appreciate any suggestions.
You should use :
intNumberOfRows = dgvWellsFargo.RowCount - 1;
The reason is, if you have 5 rows in your gridview, then the count will be 5 and if you know, row index starts with 0. So dgvWellsFargo.Rows[intNumberOfRows] means dgvWellsFargo.Rows[5], but you have max row index 4
Also, you have 2 cells, so the cell index should be used Cells[0] and Cells[1]
So your final code should be :
int lastRowIndex = dgvWellsFargo.RowCount - 1;
txtNoOfTransactions.Text = dgvWellsFargo.Rows[lastRowIndex].Cells[0].Value.ToString();
txtTotal.Text = String.Format("{0:c2}", dgvWellsFargo.Rows[lastRowIndex].Cells[1].Value.ToString());

how can i calculated values after Worksheet.Calculate()?

i tried Trial version of Gembox.SpreadSheet.
when i Get Cells[,].value by for() or Foreach().
so i think after Calculate() & get Cell[].value, but that way just take same time,too.
it take re-Calculate when i Get Cell[].value.
workSheet.Calcuate(); <- after this, values are Calculated, am i right?
for( int i =0; i <worksheet.GetUsedCellRange(true).LastRowIndex+1;++i)
{
~~~~for Iteration~~~
var value = workSheet.Cells[i,j].Value; <- re-Calcuate value(?)
}
so here is a Question.
Can i Get calculated values? or you guys know pre-Calculate function or Get more Speed?
Unfortunate, I'm not sure what exactly you're asking, can you please try reformulating your question a bit so that it's easier to understand it?
Nevertheless, here is some information which I hope you'll find useful.
To iterate through all cells, you should use one of the following:
1.
foreach (ExcelRow row in workSheet.Rows)
{
foreach (ExcelCell cell in row.AllocatedCells)
{
var value = cell.Value;
// ...
}
}
2.
for (CellRangeEnumerator enumerator = workSheet.Cells.GetReadEnumerator(); enumerator.MoveNext(); )
{
ExcelCell cell = enumerator.Current;
var value = cell.Value;
// ...
}
3.
for (int r = 0, rCount = workSheet.Rows.Count; r < rCount; ++r)
{
for (int c = 0, cCount = workSheet.CalculateMaxUsedColumns(); c < cCount; ++c)
{
var value = workSheet.Cells[r, c].Value;
// ...
}
}
I believe all of them will have pretty much the same performances.
However, depending on the spreadsheet's content this last one could end up a bit slower. This is because it does not exclusively iterate only through allocated cells.
So for instance, let say you have a spreadsheet which has 2 rows. The first row is empty, it has no data, and the second row has 3 cells. Now if you use 1. or 2. approach then you will iterate only through those 3 cells in the second row, but if you use 3. approach then you will iterate through 3 cells in the first row (which previously were not allocated and now they are because we accessed them) and then through 3 cells in the second row.
Now regarding the calculation, note that when you save the file with some Excel application it will save the last calculated formula values in it. In this case you don't have to call Calculate method because you already have the required values in cells.
You should call Calculate method when you need to update, re-calculate the formulas in your spreadsheet, for instance after you have added or modified some cell values.
Last, regarding your question again it is hard to understand it, but nevertheless:
Can i Get calculated values?
Yes, that line of code var value = workSheet.Cells[i,j].Value; should give you the calculated value because you used Calculate method before it. However, if you have formulas that are currently not supported by GemBox.Spreadsheet's calculation engine then it will not be able to calculate the value. You can find a list of currently supported Excel formula functions here.
or you guys know pre-Calculate function or Get more Speed?
I don't know what "pre-Calculate function" means and for speed please refer to first part of this answer.

Reading data from datagridview and saving into list

I have a C# program that stores some data in a datagridview called DGV.
DGV has 6 columns and has the following structure:
Column1: SALES_US
Column2: QUANTITY_US
Column3: SALES_EU
Column4: QUANTITY_EU
Column5: SALES_AS
Column6: QUANTITY_AS
The programs fills the columns labelled as "SALES" (e.g. columns 1-3-5) with the name of some items. Then in the columns labelled as "QUANTITY" (e.g. columns 2-4-6) the user input some numbers only if some of the item has been sold.
E.g. if in row 1 I have the item A, in cell[1, 0] (so row 0, column 1, supposing that rows/columns are indexed starting from 0) I can put 1000 if 1000 items A have been sold in US, and so on.
So, after user input, the DGV will have in column 1-3-5 (again, supposing that index starts at 0) some numbers but it is also possible that some cells are empty.
So I need to loop through the columns where the user has input the quantities and store the quantities in a list (in my example below I have created a list of string, called "myKeys").
Is there a quick/efficient way of doing this? Please consider that I am not super expert and so far I have only found the following (which works but seems very slow).
class organizer
{
public List<string> organize(DataGridView DGV, DataGridView DGV2,
DataGridView DGV3)
{
List<string> myKeys = new List<string>(); //This is the list of quantities
// the cells of DGV are accessible using [col, row] coordinates
for (int counterRows = 0; counterRows < countRows; counterRows++)
{
try // look for data in the second column
{
test = DGV[1, counterRows].Value.ToString(); // if the cells is empty the string conversion is not possible and no info will be stored
myKeys.Add(DGV[1, counterRows].Value.ToString());
}
catch{}
try // look for data in the fourth column
{
test = DGV[3, counterRows].Value.ToString(); // if the cells is empty the string conversion is not possible and no info will be stored
myKeys.Add(DGV[3, counterRows].Value.ToString());
}
catch{}
try // look for data in the sixth column
{
test = DGV[5, counterRows].Value.ToString(); // if the cells is empty the string conversion is not possible and no info will be stored
myKeys.Add(DGV[5, counterRows].Value.ToString());
}
catch{}
}
// here the same for-loop is repeated to add the quantities to the list reading from the other datagridviews.
return myKeys;
}
}
In this case I loop through the cells with a for loop and use the try-catch to see if the value can be converted to string. If the cell is empty, the ".ToString()" will fail and the following line that add the cell value in the list "myKeys" is not executed.
But I need to specify directly that I first work on column 1, the 3 and finally 5.
Apart from the (probably) useless code duplication, the code seems to run quite slow for such a small datagridview (I have less than 50 rows). I need to do this on other datagridviews, applying same logic.
Your suggestion about better way to deal with the problem are very appreciated.

DataGridView show row on bottom

I show the next selected row on top of grid using FirstDisplayedScrollingRowIndex property like this:
int currentIndex = dgvMain.FirstDisplayedScrollingRowIndex;
int[] highlightedArray = HighlightedRows.ToArray();
highlightedArray = highlightedArray.OrderBy(h => h).ToArray();
var next = highlightedArray.FirstOrDefault(r => r > currentIndex);
dgvMain.FirstDisplayedScrollingRowIndex = next;
but how can I show the next selected row on bottom of grid? I searched for a property but didn't find anything.
If there was a method or property which could tell me how many rows I see at a time, it would be helpful, something like:
dgvMain.FirstDisplayedScrollingRowIndex = next + dgvMain.RowsPerView;
lets say I see 10 rows per view, and I want to show 36th row on bottom of my grid, in this situation showing the 27th row (36 - 9) on top would do the trick, because there are 9 other rows (10 - upper row) below it.
You could use the DataGridView.DisplayedRowCount()
See this MSDN for more details

How to change a datagridview's columns position and its index value?

Here i like to change columns position with its index value of my datagridview... i tried to change my column index with following suggested code,
private void button17_Click(object sender, EventArgs e)
{
dataGridView1.Columns[0].DisplayIndex = 1;
dataGridView1.Columns[1].DisplayIndex = 0;
var head1 = dataGridView1.Columns[0].HeaderText;
var head2 = dataGridView1.Columns[1].HeaderText;
MessageBox.Show(head1,"column0");
MessageBox.Show(head2,"column1");
}
its just changing columns display position, but not the columns index value.. i have given my output screenshots here.
i need to interchange the positions with index of those two highlighted columns named as TIMESTAMP and WINDSPEEDHEIGHT1.
columns display position has been changed as above,
but still index value of column0 is TIMESTAMP and index value of column1 is WINDSPEEDHEIGHT1....
Can anyone help regarding interchanging the columns position with the index value..?
thanks in advance.
Just switch the call on them in your query.

Categories

Resources