How to dynamically adjust table column width in RDLC report

16.1k views Asked by At

I have a RDLC report in C# which displays a table.

I am providing a facility to the user to select the required columns to be displayed in the report.

So when the number of columns in the report are reduced half of the page on the right side appears blank due to which the presentation of the report looks bad.

I want to find out a way using which either I can set the column size of the visible columns dynamically.

OR

I can change the table location so that the table is displayed in the centre of the page.

So far I have found that I cannot write an Expression or pass parameter to set the size or location of a control in RDLC Report.

I would like to know if there is alternative way of achieving this.

4

There are 4 answers

0
Raphael On

What you could do is to change the rdlc at runtime, it is just and xml file, so you can parse it and go to set the width of the table programmatically. I use this approach to translate my reports for multilanguage and it works fine.

This link shows you how to translate the report, but this code is a good starting point. I think you can easily customize it for your purpose. Change RDLC XML

0
LUCKY MOSERI On

How To adjust each column size, Check Image

Click any column header and you will see the structure of that table will be visible just above the table in gray, from there you will be able to adjust your column size. Refer to the attached image, the top part pointed "red arrow" that's your table structure and that's where you modify the structure of your table. Hope it helps.

1
Poppert On

This might be an alternative:

  1. Make columns small
  2. Add group row
  3. In the top group row, add image that will be used to "push" col width of both rows in the group
  4. On the image properties, set it to use External image source and use a report parameter to set which image to use
  5. On image, set "keep original size"

Edit: You may also generate such "padding image" dynamically to allow different widths, for example in MVC:

public class ImageUtilController : Controller
{
    public FileContentResult GenerateTransparentRectangle(int width, int height)
    {
        var image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        using (var g = Graphics.FromImage(image))
        {
            g.Clear(Color.Transparent);
            g.FillRectangle(new SolidBrush(Color.Transparent), 0, 0, width, height);
        }
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return File(ms.ToArray(), "image/png");
    }
}
0
Rupesh On

To make visible columns expand and fill the space of excluded (hidden) columns so that report width is maintained, work needs to be done is as under.

The rdlc file is an xml document. It defines column widths as following xml (This is specific to RDLC file created using Visual Studio 2005, it may differ for other version). Below xml suggests that there are 6 columns in the table.

    <TableColumns>
      <TableColumn>
        <Width>0.5in</Width>
      </TableColumn>
      <TableColumn>
        <Width>1.125in</Width>
      </TableColumn>
      <TableColumn>
        <Width>1in</Width>
      </TableColumn>
      <TableColumn>
        <Width>1in</Width>
      </TableColumn>
      <TableColumn>
        <Width>0.5in</Width>
      </TableColumn>
      <TableColumn>
        <Width>1.375in</Width>
      </TableColumn>
    </TableColumns>

Basically logic is simple, just increase the width of visible columns. But its implementation requires few lines of code.

  1. Calculate the sum of width of hidden columns and then re-calculate width of visible columns

        float[] resizedwidth;
        // code for recalculation goes here
    
  2. Read entire report xml into string variable 'rptxml'

        String rptxml = System.IO.File.ReadAllText(@"D:\SO\WinFormQ\WinFormQ\Report1.rdlc");
    
  3. Replace above xml segment with modified xml segment

        int start = rptxml.IndexOf("<TableColumns>");
        int end = rptxml.IndexOf("</TableColumns>") + "</TableColumns>".Length;
        String resizedcolumns = String.format(
            "<TableColumns>"
            + "<TableColumn><Width>{0}in</Width></TableColumn>"
            + "<TableColumn><Width>{1}in</Width></TableColumn>"
            + "<TableColumn><Width>{2}in</Width></TableColumn>"
            + "<TableColumn><Width>{3}in</Width></TableColumn>"
            + "<TableColumn><Width>{4}in</Width></TableColumn>"
            + "<TableColumn><Width>{5}in</Width></TableColumn>"
            + "</TableColumns>"
            , resizedwidth[0], resizedwidth[1], resizedwidth[2], resizedwidth[3], resizedwidth[4], resizedwidth[5]
            );
        rptxml = rptxml.Substring(0, start) + resizedcolumns + rptxml.Substring(end);
    
  4. Create TextReader from string variable 'rptxml'

        TextReader tr = new StringReader(rptxml);            
    
  5. Use LoadReportDefinition() method to load the modified report definition

        reportViewer1.LocalReport.LoadReportDefinition(tr);
    

Continue with specifying DataSources and ReportParameters etc. and finally display the report. NOTE: Don't forget to close TextReader tr.Close().