How to set column width to autofit in Kendo Grid MVC

做~自己de王妃 提交于 2020-01-04 03:18:11

问题


I use to my page of Kendo grid that have multi columns.
I want set a column width to autofit and change width automatically.
Plz help me , thanks

@(Html.Kendo().Grid<GardeshKar.Models.v_marhaleh_marahel>()
    .Name("grdMarahel_Gardeshkar")
    .ToolBar(toolbar => toolbar.Custom().Name("btnAddMarhaleh").Text("اضافه").HtmlAttributes(new { id = "btnAddMarhaleh", href = "#" }))
    .Columns(columns =>
    {
        columns.Bound(c => c.Code).Width(50).Title("کد");
        columns.Bound(c => c.Desc).Width(150).Title("شرح");//Autofit
        columns.Command(c => c.Destroy().Text("حذف")).Width(70).Title("عملیات");
    })
    .HtmlAttributes(new { style = "height: 380px;" })
    .Scrollable()
    .Sortable()
    .Resizable(rl => rl.Columns(true))
    .Selectable(sl => sl.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(m => m.Id(p => p.id))
        .Read(read => read.Action("Get_DS_Marahel_Gardeshkar", "Home",
                    new { intGardesh = ViewBag.CodeGardeshKar }))
        .Destroy(del => del.Action("Del_Marhaleh_GardeshKar", "Home")))
)  

that column specific with autofit must column width change to autofit


回答1:


One way is to remove the "Scrollable" property or you can use this function in dataBound event.

        function Autocolumnwidth(e) {
            var grid = e.sender;
            for (var i = 0; i < grid.columns.length; i++) {
                 grid.autoFitColumn(i);
            }
        }


来源:https://stackoverflow.com/questions/28535967/how-to-set-column-width-to-autofit-in-kendo-grid-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!