问题
How to have table size fixed in SSRS? I have tried this How to set Fixed Rows of Tablix in SSRS but, my concern is How to add blank rows if data row are lesser than 5, in the report where ,for example 15 rows can be fitted in. Rather than insert new rows by static, I want to display it dynamically. As in, we don't always know how many data rows will be there. I can think of setting row visibility with expressions. But, I am not quite sure and I want to understand more about it. Another alternate way might be using stored procedure?? I would like to know more about it too. I would appreciate any insight on this matter. Thanks
回答1:
In this answer the max row is 5 because 15 to a bit long and the number of data rows is 3.
Target output:
*I only have 3 data so the remaining other two rows should be empty because the max row is 5
Solution:
1. First create a table. Add header/column name if needed.
2. Reference your dataset on the table(right click on the top left most of the table then select Tablix Properties)
- Under General select Dataset Name dropdown of the dataset that you need then click OK.
- On the data row add the data you need in just the normal way to add
- Now on the left most of the data row right click > Insert Row > Outside Group Below.
Do this 5 times. This is the target output.
Now on the first row of the outside of the group. On the left most right click > Row Visibility. On the expression add this.
- Do this also on the other outer row but the values should be:
=IIF(CountRows() < 3, False, True) -> for the second row
=IIF(CountRows() < 4, False, True) -> for the third row
=IIF(CountRows() < 5, False, True) -> for the fourth row
=IIF(CountRows() < 6, False, True) -> for the fifth row
*The visibility will hide the row outside the group depending on the count of rows in that group. With this even if you have empty dataset the default number of rows is always 5.In your case you add 15 blank rows outside the group with the same height in the row with data and add visibility condition for each.
UPDATE:
To limit the number of data to show in your table this is how I did it.
- The easiest way is to use
SELECT TOP 5
or 15 in your case in your sql query to make sure you always get the exact number of records to show in your table.
The other way is to use filter in your table.
- I added RowCount column in my sql query to count all the number of records that I have.
- On your report right click on the upper left corner of your table > Tablix Properties > Filters
- Then I entered this value to limit the number of data to be shown in my table.
From this hopefully you will now have an idea how to do it in your report.
回答2:
I used to apply logic of adding additional rows where Invoices or Letters are needed to be designed and should always span to complete page irrespective of detail lines. However, I soon changed my approach due to various issues. If you are also in the process of creating letter or invoice, read on; I don't fix table size, however I fix report layout. This way even if there are less number of rows or more rows, SSRS handles data properly. Explanation is as given below,
Let's assume that you are creating a letter with width 8.5 inch and height 11 inch. Assuming you have left 0.25 inch of border from all side; that leaves you with 8 inch of body width and 10.5 inch of height. Leave 1.5 inch for page header and 1 inch for page footer from 10.5 inch height. That leaves you with 8 inch of body height. Explicitly fix the height of Body to 8 inch. Once this is done, SSRS understands that it has to generate a page of 10.5 inch irrespective of detail lines coming from table placed in body. I hope this helps. Cheers!
P.S. This approach will only give expected results only on PDF output.
回答3:
@bot's answer is the closet. After finding on web, I stumbled across with an article but you've got to use stored procedure(separate dataset for tablix only) which calculate out to numbers of lines/rows to fill inside the tablix, to meet your desired result. P.S: I don't remember the exact URL reference.
Stored procedure sample:
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[uspPurchaseLines]
(
--@InInvoiceNbr int
@InLinesPerPage int
)
AS
DECLARE @TotalRows int
DECLARE @Remainder int
DECLARE @NumPages int
DECLARE @NextPageRows int
set @TotalRows= 0
SELECT
ROW_NUMBER() OVER( ORDER BY P_id)as InvoiceRow,
CusID,
P_id,
Inv_No,
P_Desc,
Del_date,
Qty,
Pack_size,
U_Prize,
Amt
into #tempInvoice
FROM Purchase_Details
SET @TotalRows= @@ROWCOUNT
IF @TotalRows=0
BEGIN
WHILE @TotalRows < @InLinesPerPage -- Add Blank Rows will generate blank invoice.
BEGIN
SET @TotalRows= @TotalRows+1
INSERT #tempInvoice
(InvoiceRow,
CusID,
P_id,
Inv_No,
P_Desc,
Del_date,
Qty,
Pack_size,
U_Prize,
Amt
)
VALUES
(@TotalRows
--,@InInvoiceNbr
,''
,''
,0
,''
,NULL
,0
,''
,0
,0
)
END
END
ELSE
BEGIN
SET @Remainder = @TotalRows%@InLinesPerPage -- get remainder
IF @Remainder !=0
BEGIN
-- Get the current page increase by 1 becasue we have a remainder.
SET @NumPages = @TotalRows/@InLinesPerPage +1
SET @NextPageRows = @NumPages * @InLinesPerPage
WHILE @TotalRows < @NextPageRows -- Add Blank Rows
BEGIN
SET @TotalRows= @TotalRows+1
INSERT #tempInvoice
(InvoiceRow,
CusID,
P_id,
Inv_No,
P_Desc,
Del_date,
Qty,
Pack_size,
U_Prize,
Amt
)
VALUES
(@TotalRows
--,@InInvoiceNbr
,''
,''
,0
,NULL
,''
,0
,''
,0
,0
)
END
END
END
SELECT * from #tempInvoice order by InvoiceRow asc
return
来源:https://stackoverflow.com/questions/36642141/ssrs-fixed-tablix-add-blank-rows-below