- Home
- Forum
- ASP.NET Web Forms
- Need to access column properties in code behind grid rowbound event
Need to access column properties in code behind grid rowbound event
I am dynamically binding the grid to the datasource property with a stored proc result set from DB using Business object in the code behind file.
.aspx.cs
FlatGrid.DataSource = sampleBO;
FlatGrid.DataBind();
FlatGrid.DataSource = sampleBO;
FlatGrid.DataBind();
.aspx
<ej:Grid ID="FlatGrid" runat="server" OnServerExcelExporting="FlatGrid_ServerExcelExporting" AllowPaging="True" AllowFiltering="True" AllowSorting="True"
AllowGrouping="True" AllowResizing="True" AllowResizeToFit="True" AllowScrolling="true" AllowReordering="True" CssClass="" DataSourceCachingMode="None"
EnableLoadOnDemand="False" MinWidth="0" Width="1000px" SelectionType ="Multiple" >
<ClientSideEvents DataBound="bound" />
<ResizeSettings ResizeMode="NextColumn" ></ResizeSettings>
<PageSettings EnableQueryString="true" PageSize="10" />
<ToolbarSettings ShowToolbar="true" ToolbarItems="excelExport"></ToolbarSettings
</ej:Grid>
but FlatGrid.Columns.Count is showing as 0 after the databind
I want to dynamically find the Birthdate column in the code behind file. Using format property, I should be able to format the date column like dd/MM/yyyy.
something like FlatGrid.Columns["BirthDate"].Format = "{0:dd/MM/yyyy}"
<ej:Grid ID="FlatGrid" runat="server" OnServerExcelExporting="FlatGrid_ServerExcelExporting" AllowPaging="True" AllowFiltering="True" AllowSorting="True"
AllowGrouping="True" AllowResizing="True" AllowResizeToFit="True" AllowScrolling="true" AllowReordering="True" CssClass="" DataSourceCachingMode="None"
EnableLoadOnDemand="False" MinWidth="0" Width="1000px" SelectionType ="Multiple" >
<ClientSideEvents DataBound="bound" />
<ResizeSettings ResizeMode="NextColumn" ></ResizeSettings>
<PageSettings EnableQueryString="true" PageSize="10" />
<ToolbarSettings ShowToolbar="true" ToolbarItems="excelExport"></ToolbarSettings
</ej:Grid>
but FlatGrid.Columns.Count is showing as 0 after the databind
I want to dynamically find the Birthdate column in the code behind file. Using format property, I should be able to format the date column like dd/MM/yyyy.
something like FlatGrid.Columns["BirthDate"].Format = "{0:dd/MM/yyyy}"
In Asp.Net gridview control,we find the columns in the Rowdatabound server side event of the Grid, like below:
protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Width = 1000;
}
{
e.Row.Cells[0].Width = 1000;
}
How do I find the columns and format in code behind if I am binding the grid dynamically and I do not want to hard code the columns in the .aspx file?
SIGN IN To post a reply.
1 Reply
VN
Vignesh Natarajan
Syncfusion Team
January 30, 2019 10:37 AM UTC
Hi Sharmila,
Thanks for contacting Syncfusion support. We are happy to assist you.
Query: I want to dynamically find the Birthdate column in the code behind file. Using format property, I should be able to format the date column like dd/MM/yyyy.
We have achieved your requirement as an workaround by finding the property Name and value of generic class using System.Reflection name space. From that, we have checked the Date time value and applied format for that particular date column. Please refer the below code example and sample Link,
|
<ej:Grid ID="FlatGrid" runat="server" ……..">
………………..
</ej:Grid>
using System;
using System.Reflection;
……………
public partial class GridFeatures : System.Web.UI.Page
{
public static List<cols> data = new List<cols>();
public static List<Orders> order = new List<Orders>();
protected void Page_Load(object sender, EventArgs e)
{
BindDataSource();
}
private void BindDataSource()
{
……………………
this.FlatGrid.DataSource = order;
this.FlatGrid.DataBind();
PropertyFinder<Orders> PropertyFinderobj = new PropertyFinder<Orders>();
PropertyFinderobj.ModelNameAndValue(order[0]);
for (int j = 0; j < data.Count(); j++)
{
string[] formats = { "M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", "M/d/yyyy h:mm", "M/d/yyyy h:mm", "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm" };
string value = data[j].columnValue;
DateTime datetime;
if (DateTime.TryParseExact(value, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out datetime))
{
this.FlatGrid.Columns.Add(new Column() { Field = data[j].columnName, Format = "{0:dd/MM/yyyy}" }); // apply the format for date column alone
}
else
this.FlatGrid.Columns.Add(new Column() { Field = data[j].columnName });
}
}
public class PropertyFinder<Model> where Model : class
{
public void ModelNameAndValue(Model modelObj)
{
//Getting Type of Orders Class Model
Type ModelType = modelObj.GetType();
//We will be defining a PropertyInfo Object which contains details about the class property
PropertyInfo[] arrayPropertyInfos = ModelType.GetProperties();
//Now we will loop in all properties one by one to get value
foreach (PropertyInfo property in arrayPropertyInfos)
{
data.Add(new cols(property.Name, property.GetValue(modelObj).ToString()));
}
}
}
[Serializable]
public class Orders
{
. ……………..
}
[Serializable]
public class cols
{
public cols()
{
}
public cols(string columnName, string columnValue)
{
this.columnName = columnName;
this.columnValue = columnValue;
}
public string columnName { get; set; }
public string columnValue { get; set; }
}
}
}
|
For your convenience we have prepared a sample which can be downloaded from below link
Sample Link: http://www.syncfusion.com/downloads/support/directtrac/226303/ze/SyncfusionASPNETApplication497032154
Please get back to us if you have further queries.
Regards,
Vignesh Natarajan.
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
SH Sharmila
- Jan 29, 2019 04:37 AM UTC
- Jan 30, 2019 10:37 AM UTC