KeepWith throws Object reference not set to an instance of an object - pdfsharp

Hoping someone out there might have had the same issue as this and have found a resolution.
I'm trying to set certain rows within a table to be grouped so that if a row falls into the next page the other rows will also go across to that next page.
I'm using PdfSharp and Migradoc setting the rows KeepWith property to the index of the row
i want to keep attached to.
IE:
var row = myTable.AddRow();
var rowIndex = row.Index;
foreach (var term in terms)
{
row = myTable.AddRow();
if (term.name == "group")
{
row.KeepWith = rowIndex;
}
}
When i try and load the pdf I get an 'Object reference not set to an instance of an object'
error thrown and cannot figure out why. Any help would be great.

Set KeepWith to the count of following rows you want to keep with the current row. To keep three rows together, the correct value is 2 (set at the first row of the group).

I was making the mistake of putting the KeepWith at the end of the rows to keep together. As Vive la déraison said, place the KeepWith on the first row of the rows to keep together.
Additionally, the int provided for the KeepWith value does NOT include the row it is set on. So to keep rows rowA, rowB, and rowC together, you would set:
rowA.KeepWith = 2;
If you're setting a KeepWith for the last rows of your table, and accidentally set the value +1 (Inclusive of the row you set it on), you will get the same "Object Not Set..." error, as you are extending the KeepWith longer than the table rows.
Gotta love those Off-By-One errors.

Related

VB.Net - How to compare 2 columns in GridView as numbers?

I have a GridView which I plan to add different colors in each row based on certain conditions.
One such condition is to color the row in a specific color if a number in column 6 is higher than the number in column 7.
If e.Row.Cells(6).Text > e.Row.Cells(7).Text Then
e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")
Else
However, when I test the code it seems to only change the color when the left most value in column 6 is larger than column 7, regardless of the fact that column 6 is in the hundreds, while column 7 is in the thousands.
I believe the cause is coming from me using text in the If statement. However, any instance of number I can think to change doesn't seem to fit into the code:
If e.Row.Cells(6).Number > e.Row.Cells(7).Number Then
or
If e.Row.Cells(6).Integer > e.Row.Cells(7).Integer Then
These example gives me a message saying
'Number' is not a member of 'System.Web.UI.WebControls.TableCell'
What else can I use that is compatible with the current code?
I believe that you are missing a casting. I'm not sure if you're comparing only Integers or complex numbers, if you don't know I recommend you to cast directly to doubles:
If Convert.ToDouble(e.Row.Cells(6).Text) > Convert.ToDouble(e.Row.Cells(7).Text) Then
e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")
Else
Convert.ToDouble will convert any text to number.
More info about casting:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number
https://www.dotnetheaven.com/article/casting-integer-to-long-single-and-double-using-vb.net
If you might have some empty values you can use the TryCast:
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/trycast-operator

Access 2010 Calculate Crash on column addition

I am doing a calculate field in a table in access 2010. A column being the total is adding up to 24 other columns. I either get no return or it will crash.
I have the following in the expression builder that gives no return
[Column1]+[Column2]+[Column3]+[Column4]+[Column5]... etc
If I add an = as below, access will crash.
=[Column1]+[Column2]+[Column3]+[Column4]+[Column5]... etc
Note, both do not literally include '... etc' I just did not want to clog up the question with the column names.
I would like this to return the values of the columns being added together and totaled up. What is missing or required for this?

BIRT, How to get Dataset Row Count using Javascript

How can I get Dataset Row Count from Javascript function in BIRT. I tried searching this in BIRT exchange, but the only solution offered there is to have a new dataset getting count of values of required data set. This wont suit my needs.
Is there any way to obtain it using dataset events.
An easy way would be to count dataset items in a report variable.
Declare a new variable in your report outline:
Reset it in beforeOpen script of the dataset (in case this dataset is invoked multiple times during report execution):
vars["items"]=0;
Increment the variable in onFetch script of the dataset:
vars["items"]++;
Use your variable in any expression. For example add a dynamic text element in report's body such:
"Items count="+vars["items"]
Important 1: This approach works if and only if the dataset is bound to at least one report element (a table, chart, data element, etc.). For example, it won't work if the dataset is only invoked to fill a list of selection choices of a report parameter.
Important 2: In the body of the report, this variable can only be used after the first report element using the relevant dataset, otherwise it won't be initialized
Dominique has a great answer; It is not clear to me from your question if this simpler solution might also meet your needs.
In your data set use a computed column with a value of '1', then sum the values.
You can write JS that only adds the value if specific criteria are met.
Or you can use an aggregation on your report to sum the values, which would be after any filters or groups are placed.
There is a simpler way if you use the row number in your tables footer:
In the 'Dynamic Text' element you can select:
Avilable Column Bindings > Table > RowNum
Add 1 as the index starts with 0.
You could also make a variable and then add to that for each row created in a table.
For example, in the Table-Detail script, set an onCreate event to check for a value in each row and if there is one to increase the row count. The following onCreate script would check if the row is empty. If the row is not empty, the script increases the counter and goes to the next row.
var checker = this.getRowData().getExpressionCount();
if( checker > 0 ) vars["Counter"]++;
Then you could add dynamic text after the table with the following expression:
"Row count="+vars["Counter"]

Strange QTableWidget behavior - not all cells populated after sorting followed by repopulating

I have a QTableWidget that I populate like this:
// Clear the table
this->topPatchesTableWidget->setRowCount(0);
this->topPatchesTableWidget->setRowCount(numberToDisplay);
for(unsigned int pairId = 0; pairId < numberToDisplay; ++pairId)
{
// Display patch match scores
QTableWidgetItem* myLabel = new QTableWidgetItem;
myLabel->setData(Qt::DisplayRole, myValues[pairId]);
this->tableWidget->setItem(pairId, 0, myLabel);
... fill other columns ...
}
(I have some other UI elements to set properties that compute the values in myValues). If I change the properties, recompute, and recreate the table, everything works as expected. If I sort the table by clicking on one of the headers, it sorts correctly. HOWEVER, if at this point (after sorting) I click my button again to recompute the values and recreate the table, the table is very broken. That is, many of the cells are empty, and the cells that are not empty don't seem to be in any particular order.
By adding a manual call to
this->tableWidget->sortByColumn(0, Qt::AscendingOrder);
at the beginning of my CreateTable function, everything works as expected, but of course the newly created table is sorted by column 0 rather than the column that was selected for the last sort.
Does anyone have any idea why things would go so wrong without the call to sortByColumn? (I tried to make a simple example but I can't replicate the problem in a demo program).
Thanks,
David
I had a similar problem in python, if a column header was selected to enable sorting the cells after that column stopped populating.
I got around it by setting self.tableWidget.setSortingEnabled(False) at the beginning of the row add method, then setting it back to self.tableWidget.setSortingEnabled(True) at the end of the row add method. According to riverbank computing this is the officially recommended way to address this problem.
QTableWidget.setItem (self, int row, int column, QTableWidgetItem item)
Note that if sorting is enabled (see sortingEnabled) and column is the current sort column, the row will be moved to the sorted position determined by item.
If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).
See also item() and takeItem().
This is an elaboration on Karnisov's answer, also with PyQt. Disabling sorting before doing any setItem()s also worked for me, but it caused the user's sort selection to be lost, so I did it like this, where t is a QTableWidget:
oldSort = t.horizontalHeader().sortIndicatorSection()
oldOrder = t.horizontalHeader().sortIndicatorOrder()
t.setSortingEnabled(False)
# make the changes
t.sortItems(oldSort, oldOrder)
t.setSortingEnabled(True)
Note that it is still possible for the sort to change slightly with this code. This appears to be because the sort that Qt is using here doesn't move any items that were already in order, so if we had a table like this:
NUMBER LETTER OTHER LETTER
1 B C
2 G Q
3 A Q
If we sort by NUMBER, then by OTHER LETTER, nothing changes. But if we sort by LETTER, then by OTHER LETTER, we get:
NUMBER LETTER OTHER LETTER
1 B C
3 A Q
2 G Q
So if we had previously sorted by LETTER, then OTHER LETTER, this code would only do a single sort by OTHER LETTER, and we'd end up with the second version of the table instead of the first (assuming that the order the elements were added to the table in matches NUMBER). In this spot in my code, all items are deleted and re-added to refresh the table, so this can sometimes cause noticeable changes in order.
I had the same problem - always, when I wanted to insert new data, i buildet the header and setSortingEnabled(true). I guess the QTableWidget does not like several calls for setSortingEnabled(true). So I put it into the constructor of my MainWindown, and thats it!
I never figured out what was wrong with this. I gave up and implemented a custom model/view which is probably a much better idea anyway.

How to sort QtableWiget for particular row range in QT

I want to sort Qtable widget by column but first row should not be considered while sorting( means sorting should be done from 2nd row onwards)
Is there any way to use sortbycloumn() method of Qtable Widgetfor particular row range or is there any other way in QT to sort QTableWidget from 2nd row onwards.
I would probably do something along these lines:
Subclass QTableWidgetItem
Re-implement the < operator
Set a special flag on all of the items in the first row (using setData())
Then you can do something like this:
bool MyTableWidget::operator <(MyTableWidgetItem *other)
{
if( specialFlag )
return true;
return QTableWidgetItem::operator<(other);
}
This way your first row is still being sorted, but you've forced it to always be considered "less" than every other.
In order to keep your row from going to the bottom when you change sorting directions (ascending vs descending), you can change the behavior of the < function by grabbing the header view from your table widget and asking it which direction your sorting.
See:
QTableWidgetItem::tableWidget()
QTableWidget::headerView()
QHeaderView::sortIndicatorOrder()

Resources