This is a migrated thread and some comments may be shown as answers.

Print RadGrid contents

6 Answers 230 Views
Grid
This is a migrated thread and some comments may be shown as answers.
LG
Top achievements
Rank 2
LG asked on 09 Dec 2009, 09:35 PM
I tried the following solution but unfortunately, only the visible rows - displayed at the top of the grid - are printed, not those that you see on the screen after scrolling down. AutoGenerateColumns and AllowPaging are set to false.

http://www.telerik.com/community/code-library/aspnet-ajax/grid/print-radgrid-contents.aspx

function PrintRadGrid() 
    var previewWnd = window.open('about:blank', '', '', false); 
    var sh = '<%= ClientScript.GetWebResourceUrl(radGrid1.GetType(),String.Format("Telerik.Web.UI.Skins.{0}.Grid.{0}.css",radGrid1.Skin)) %>'
    var styleStr = "<html><head><link href = '" + sh + "' rel='stylesheet' type='text/css'></link></head>"; 
    var htmlcontent = styleStr + "<body>" + $find('<%= radGrid1.ClientID %>').get_element().outerHTML + "</body></html>"; 
    previewWnd.document.open(); 
    previewWnd.document.write(htmlcontent); 
    previewWnd.document.close(); 
    previewWnd.print(); 
    previewWnd.close(); 


I'm using Visual Studio 2008 / .Net 3.5 along with Telerik RadControls for ASP.NET AJAX 2009.3.1103.35

Any thoughts?

Thanks

6 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 09 Dec 2009, 09:59 PM
Hello RJ,

Please try the project, attached to this post and let me know whether it is suitable for you.\
 
Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
LG
Top achievements
Rank 2
answered on 09 Dec 2009, 11:37 PM
Thanks, Daniel but no luck so far. The Print feature works in the solution that you have provided but not in mine.

I'm using nested master pages and AutoGenerateColumns is set to false. Would that be the cause?

RJ


0
LG
Top achievements
Rank 2
answered on 10 Dec 2009, 06:13 PM

OK. I was able to implement this printer-friendly solution for the entire web page but I still have a problem with my RadGrid. It only displays the first 18 rows with the total (footer) but ..... the scrolling bar still appears on the right hand side of the grid.

http://www.eggheadcafe.com/articles/20030627a.asp

Is there a way to get rid of the RadGrid scrolling bar and show all the rows when displaying the web page in a printer-friendly mode?

Thank you
0
Daniel
Telerik team
answered on 14 Dec 2009, 01:43 PM
Hello,

The solution is shown below - in case someone stumbles upon the same problem:

$find("RadGrid1").GridDataDiv.style.height = "100%";

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Nasiphi
Top achievements
Rank 1
answered on 18 Aug 2016, 12:32 PM

I have a similar problem and I tried your solution but its only showing a blank page and not the View/DatabaseTable i want it to Print

can you please help me on how to call the table for print.

here is my Method in my Controller below

[HttpPost]
        public JsonResult getEbikesRoutesTrips()
        {
            List<eBikesRoutesTrips> eBikesRT = new List<eBikesRoutesTrips>();
            {

                string query = "SELECT RouteFrom, RouteTo, COUNT(usageID) as Trips  FROM eBikeUsage GROUP BY RouteFrom, RouteTo";//Query
                string connectionString = "Enter connection string here"
                using (SqlConnection cn = new SqlConnection(connectionString))
                {
                    using (SqlCommand cm = new SqlCommand(query, cn))
                    {
                        cn.Open();
                        using (SqlDataReader reader = cm.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                eBikesRT.Add(new eBikesRoutesTrips
                                {
                                    RouteFrom = reader["RouteFrom"].ToString(),
                                    RouteTo = reader["RouteTo"].ToString(),
                                    Trips = reader["Trips"].ToString(),
                                });
                            }
                        }
                    }
                }
            }
           
            return Json(eBikesRT); //we return the list eBikes list please note the name eBikes the same is used in the java script written in the index view

        }
        //Show All Method retrieves all adata in Reservations table
        [HttpGet]
        public ActionResult geteBikesTable(string sortOrder, string currentFilter, string searchString, int? page)
        {
  

            using (uYilo_R_S_Model db = new uYilo_R_S_Model())
            {
                List<Reservation> eBikesTable = new List<Reservation>();
                {

                    string query = "SELECT * From Reservation";//Query
                    string connectionString = ConfigurationManager.ConnectionStrings["uYilo_R_S_Model_Conn_String"].ConnectionString;//connection string found in the Web.config file
                    using (SqlConnection cn = new SqlConnection(connectionString))
                    {
                        using (SqlCommand cm = new SqlCommand(query, cn))
                        {
                            cn.Open();
                            using (SqlDataReader reader = cm.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    eBikesTable.Add(new Reservation
                                    {
                                        studentName = reader["studentName"].ToString(),
                                        studentNo = reader["studentNo"].ToString(),
                                        RouteFrom = reader["RouteFrom"].ToString(),
                                        RouteTo = reader["RouteTo"].ToString(),
                                        ReservationDate = reader["ReservationDate"].ToString(),
                                        timeSlot = reader["timeSlot"].ToString(),
                                        eBikeNumber = reader["eBikeNumber"].ToString(),
                                        //DateCreated = reader["DateCreated"].ToString(),
                                    });
                                }
                            }

                        }
                    }
                }

                ViewBag.CurrentSort = sortOrder;
                ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
                ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

                if (searchString != null)
                {
                    page = 1;
                }
                else
                {
                    searchString = currentFilter;
                }

                ViewBag.CurrentFilter = searchString;

                var reservations = from s in db.Reservation
                                   select s;

                if (!String.IsNullOrEmpty(searchString))
                {
                    reservations = reservations.Where(s => s.studentName.Contains(searchString)
                                           || s.studentName.Contains(searchString));
                }




                //else if (!String.IsNullOrEmpty(searchString))
                //{
                //    reservations = reservations.Where(s => s.ReserverIdentity.Contains(searchString)
                //                           || s.ReserverIdentity.Contains(searchString));
                //}

                switch (sortOrder)
                {
                    case "name_desc":
                        reservations = reservations.OrderByDescending(s => s.studentName);
                        break;
                    case "Staff/Stud_Number":
                        reservations = reservations.OrderBy(s => s.studentNo);
                        break;
                    case "date_desc":
                        reservations = reservations.OrderByDescending(s => s.ReservationDate);
                        break;
                    case "RouteFrom":
                        reservations = reservations.OrderByDescending(s => s.RouteFrom);
                        break;
                    case "RouteTo":
                        reservations = reservations.OrderByDescending(s => s.RouteTo);
                        break;
                    case "Time":
                        reservations = reservations.OrderByDescending(s => s.timeSlot);
                        break;
                    default:
                        reservations = reservations.OrderBy(s => s.eBikeNumber);
                        break;
                }
                int pageSize = 12;
                int pageNumber = (page ?? 1);
                return View(eBikesTable.ToPagedList(pageNumber, pageSize)); //we return the list eBikes list please note the name eBikesTable the same is used in the java script written in the index view
            }
        }

 

and my here is my view:

 <table id="reservationID" class="table table-striped table-bordered" cellspacing="0" width="100">
        <tr>
            <th>
                @Html.ActionLink("Name", "geteBikesTable", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
            </th>
            <th>
                Student Name
            </th>

            <th>
                Route To
            </th>
            <th>

                Route From
            </th>
            <th>

                Time Slot
            </th>
            <th>

                Reservation Date
            </th>


            <th>

                eBike Number
            </th>

            <th></th>
        </tr>

        @foreach (var item in Model)
            {
            <tr>

                <td>
                    @Html.DisplayFor(modelItem => item.studentName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.studentNo)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.RouteTo)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.RouteFrom)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.timeSlot)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ReservationDate)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.eBikeNumber)
                </td>

            </tr>
        }

    </table>

    <!--Print Button-->
    <input value="Print RadGrid" type="button" onclick="printGrid()" />
    <!--End-->

    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("geteBikesTable",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>

<!--PRINT Example-->
<script type="text/javascript" language="javascript">
    function printGrid()

    {
        var prWnd = window.open('about:blank', '', '', false); //Preview window
        var sh = '<%= ClientScript.GetWebResourceUrl(radGrid1.GetType(),String.Format("Telerik.Web.UI.Skins.{0}.Grid.{0}.css",radGrid1.Skin)) %>';
        var styleStr = "<head><link href = '" + sh + "' rel='stylesheet' type='text/css'></link></head>";
        var popupContent = styleStr + "<body>" + $find('<%= radGrid1.ID %>').get_element().outerHTML + "</body>";
        prWnd.document.open();
        prWnd.document.write(popupContent);
        prWnd.document.close();
        window.print();
    }
</script>
<!--End Example-->

0
Konstantin Dikov
Telerik team
answered on 23 Aug 2016, 06:51 AM
Hi Nasiphi,

This thread is for RadGrid for ASP.NET AJAX and with the view that you have you will have to find the HTML wrapping element which content you want to print. From the code snippet that you have provided, you need to modify the logic for getting the content, because the line below will try to find the element of a RadGrid control, which does not exist on your page:
var popupContent = styleStr + "<body>" + $find('<%= radGrid1.ID %>').get_element().outerHTML + "</body>";

You need to replace $find('<%= radGrid1.ID %>').get_element().outerHTML with the outerHTML of your wrapping element (assuming the "reservationID" table element).


Regards,
Konstantin Dikov
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
LG
Top achievements
Rank 2
Answers by
Daniel
Telerik team
LG
Top achievements
Rank 2
Nasiphi
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or