Ticket Q472009
Visible to All Users

GridView Custom Binding with DataTable

created 12 years ago

To fill the GridView with data I use custom bindings with sql query.
See GridViewCustomBindingGetDataHandler:

C#
public static void GetData(GridViewCustomBindingGetDataArgs e) { var command = queryBuilder.GetSqlCommand(); using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrins["Main"].ConnectionString)) { command.Connection = connection; connection.Open(); var dataTable = new DataTable(); dataTable.Load(command.ExecuteReader()); //e.Data=??? } }

The set of columns for the query is generated dynamically, so I do not have a typed container class to wrap the result.
How can I pass data to the Data property?

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hello,
Take a look at the E4394 - How to implement a simple custom binding scenario for GridView Code Central example that illustrates the simplest way to accomplish this task. I believe you can use the "dataTable" variable as the e.Data.
By the way, would you please clarify what volume of data the GridView should maintain? Possibly, it is better to use a regular binding mechanism and populate Columns automatically based on Model properties.
Take a look at the following examples, which may be helpful in this scenario:
E3530 - How to bind GridView with standard in-memory data sources (DataTable, List<T>)
E20054 - GridView - How to add a column if the AutoGenerateColumns property is set to true
UPDATED:
To get data from the DataTable class as an IEnumerable interface use dynamic types such as the ExpandoObject class. With this class, you can dynamically add required properties and set their values. I have prepared an extension method that implements the task:

C#
public static IEnumerable GetData(this DataTable dataTable) { foreach (DataRow data in dataTable.AsEnumerable()) { yield return GetElement(data, dataTable.Columns); } } static object GetElement(DataRow dataRow, DataColumnCollection columns) { var element = (IDictionary<string, object>)new ExpandoObject(); foreach (DataColumn column in columns){ element.Add(column.ColumnName, dataRow[column.ColumnName]); } return element; }
    Show previous comments (10)

      Hello,
      I believe that the initial question has been answered.
      So, I have created a new ticket on your behalf to process your inquiry more effectively:
      Q378034

      AG AG
      Armand Galagnara 11 years ago

        hi support,
        I would like to know how to be able to perform sorting and editing using the above MVC GridView binding as my project involves dynamic binding.

          Hello,
          I believe that this inquiry is different from the initial subject.
          So, I have created a new ticket on your behalf to process your question more effectively:
          Q461658: GridView - Custom Binding with sorting and editing

          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.