I have an ASPxGridView (gv) for which it has been requested that I change the default AutoFilterCondition behavior from "Begins With" to "Contains" for the text columns. In my Page_Load() I have:
foreach (GridView column in gv.Columns)
{
if (column is GridViewDataColumn)
((GridViewDataColumn)column).Settings.AutoFilterCondition = AutoFilterCondition.Contains;
}
This does work. However, we also have some columns that are Decimal and DateTime types, and since "Contains" is not a valid value for them, they end up with no selected condition; when you type in their filter box, it filters out everything (the user must manually pick a valid filter condition). I would like to be able to check for the type of data in the column within this for loop, before changing it. Is it possible to tell at this point? I could not find a way.
Any ideas?
ASPxGridView - How to determine a data type of GridViewDataColumn at runtime
Answers approved by DevExpress Support
Hello Eric,
Thank you for your clarification.
ASPxGridView does not provide a capability to directly determine a column data type. You can identify the column data type using the column FieldName property if you know data types of data base fields. If you do not know them, I suggest you implement the following approach to get the column data type:
C#foreach (GridViewColumn column in g.Columns) {
GridViewDataColumn c = column as GridViewDataColumn;
if (c != null) {
object value = grid.GetRowValues(0, c.FieldName);
if (value is DateTime){
.....
} else if (value is decimal){
.....
}else{
.....
}
}
}
Please note that ASPxGridView should be bound before you call the GetRowValues method, otherwise the control will be forcibly bound.
Let us know whether or not it meets your requirements.
Best regards,
Vladimir
There is a better aproach (IMHO):
foreach (GridViewDataColumn col in gridView.Columns)
{
GridViewDataColumnWithInfo gvdci = (GridViewDataColumnWithInfo)col;
Type type = gvdci.MemberInfo.MemberTypeInfo.Type;
if (type.Equals(typeof(String)))
{
col.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
}
}
C#foreach (GridViewColumn column in g.Columns) {
GridViewDataColumn c = column as GridViewDataColumn;
if (c != null) {
object value = grid.GetRowValues(0, c.FieldName);
if (value is DateTime){
.....
} else if (value is decimal){
.....
}else{
.....
}
}
}
with these code, what happen when there isn't any row in grid
Hello,
I already answered you in your new ticket: ASPxGridView - How to determine the column type is there is no data. Let's continue our conversation there.
Thanks,
Stanley
Hi Eric,
To determine if a column uses a text editor, compare its type with the GridViewDataTextColumn class.
Thanks,
Michael.
This does not work, since all the columns are declared as GridViewDataColumn, and when debugging, the column just shows up as GridViewDataColumn. If I compare to GridViewDataTextColumn, it returns false for all columns.
Not sure if this is important, but the DataSource is a LinqServerModeDataSource. All the columns are declared as GridViewDataColumn, and I guess the type of each column is determined at run time.
Is my only choice to go in and manually change all the column declarations to the appropriate type?