We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Grid - setting width of each column

Hi,


We are using Syncfusion grid and binding data source with view data as below. I want to set width for each column. The data contained in view data is dynamically generated and varies. In such scenario how can we set the width of each column? Please help.

 @(Html.Syncfusion().Grid<System.Data.DataRow>(str)
                        .Datasource((System.Data.EnumerableRowCollection)ViewData["vwdta_" + str])
                        .Caption(str)
                        .EnableOnDemand()
                        .TotalRecordCount(totalRecordcount)
                        .EnablePaging().PageSettings(p => p.PageSize(pagesize))
                        .EnableSorting()
                        .AutoFormat(Skins.Office2007Silver)
                        .EnableFiltering()
                        .AllowResizing(true)
                        .EnableScrolling()
                        .AllowAutoWrap(false)
                        .ClientSideEvents(c=>c.OnActionBegin("Begin"))  )


Thank you
Dikshit

1 Reply

AM Abdul Matin M Syncfusion Team February 18, 2014 05:52 AM UTC

Hi Dikshit,

 

Thanks for your interest in Syncfusion products.

 

We have analyzed your requirement and it can be achieved by iterating each column and we can set the width for each column dynamically. Please refer the below code snippet.

[CSHTML]

.Column(cols =>

    {

        if ((HttpContext.Current.Session["datasource"] as DataTable) != null)

        {

            System.Data.DataTable dataTable = (HttpContext.Current.Session["datasource"] as DataTable);

            foreach (System.Data.DataColumn col in dataTable.Columns)

            {

                cols.Add(col.ColumnName).HeaderText(col.Caption).Width(200);

            }

        }

    })

For your convenience we have prepared a simple sample based on the code snippet you have provided and the same can be downloaded from the link below.

 

Please use the sample and get back to us if you have any other queries.

 

Please let us know if you need any further assistance.

 

Regards,

Abdul Matin M


Attachment: SetWidthSample_c87dd3cb.zip

Loader.
Live Chat Icon For mobile
Up arrow icon