Ticket Q355125
Visible to All Users

ASPxGridView - How to determine a data type of GridViewDataColumn at runtime

created 13 years ago

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?

Comments (2)
DevExpress Support Team 13 years ago

    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?

      Answers approved by DevExpress Support

      created 13 years ago (modified 12 years ago)

      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

        Show previous comments (6)

          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

            DevExpress Support Team 6 years ago

              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

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.