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 As UserControl

I've created a user control and put a grid into it.
Now, i've seen on examples that it's needed to define columns and everything else on that grid.
That's not good for me because I'm getting data from store procedure and I have different colums and different select statement for reports.
That's why properties like Resize to fit don't work.

Is there a way to define that values in code? I'm using VB.net.

3 Replies

GV Gowthami V Syncfusion Team June 1, 2016 12:56 PM UTC

Hi Hrvoje, 

Thanks for using Syncfusion products. 

From your query we suspect that you are returning data from the select statement with different field and then bind it to the grid again. 

If you are doing so then for your scenario there is no need to define the columns for the grid.  

We can use the auto generated columns concept to display all the columns/fields from the assigned data source to the grid. 

Whenever you want to change the data source then assign that data source to the DataSource property of the grid and call the DataBind method in code behind itself. 

Refer to the below code example, 

//Query processing for retrieving data from data base and select command with ifferent columns 
. . . . 
. . . . 
Me.Grid1.DataSource = the data need to be bind to the grid 
. . . . 
. . . . 
<ej:Grid ID="Grid1" runat='server' AllowPaging="true" AllowSorting="true" AllowResizeToFit="true"> 
//No need column definition 

Refer to the below link for more clarification about auto generation, 

If we are misunderstood your requirement please provide us below details to understand your requirement, 

1.       How would you like to change the data source whether in code behind or you want to pass data via any post? 
2.       Do you want to generate a list of columns and grid properties in code behind. 
3.       Explain your requirement clearly. 
The provided information will help to analyze the issue and provide you the response as early as possible. 


Gowthami V. 

HV Hrvoje Voda June 1, 2016 01:02 PM UTC

Thank you for your reply.

I solved problem with auto generate columns. It works fine.
My problem remain is that columns are not resized to fit text in rows.
I'.m using AllowResizeToFit="true" but it doesn't help.
How can I resize column by their row width?

GV Gowthami V Syncfusion Team June 2, 2016 03:23 PM UTC

Hi Hrvoje, 
By default, the grid columns width get adjusted proportionally to the grid width. So if the columns count is small then the width of the columns set width space irrespective of content width. 
If more columns rendered in the grid then the column width adjusted based on the content width. In that situation if the total width of the columns exceed the grid width, then horizontal scroller will be enabled. 
So, we suspect that the reported issue is a default behavior when limited number of columns rendered. 
If we misunderstood your issue, please share the below details, 
1.       Share the screenshot of the issue. 
2.       Grid rendering code with column definition. 
The provided information will help to analyze the issue and provide you the response as early as possible. 
Gowthami V. 

Live Chat Icon For mobile
Up arrow icon