Ticket T1284150
Visible to All Users

How can i make my grid with column-unit Data??

created 4 days ago

i'm wondering that i can make grid with column-unit data.

My Data is like as below.

C#
public class DatasetData { public string Name { get; set; } public int Index { get; set; } public string Time { get; set; } public object Data { get; set; } }
C#
List<DatasetData> ColData; List<List<DatasetData>> WholeData;
Razor
<DxGrid data="@ColData" CssClass="grid-size" VirtualScrollingEnabled="true" TextWrapEnabled="false" AllowSort="false"> <Columns> <DxGridDataColumn FieldName="Data"/> </Columns> </DxGrid>

If i make GridColumn with ColData, then it should be like 1-Column Grid.
And I want to make dynamic Grid with multi-ColData.

If there's single data in WholeData, then 1-column Grid, and if there are multiple datas in WholeData, then multi-column Grid.

Help me pleeeease…

Thanks.

Answers approved by DevExpress Support

created 4 days ago

Hello,

Thank you for contacting us.

DxGrid displays rows for each element in the bound collection. You need to select another data structure (e.g., DataTable) to be able to bind it to DxGrid. You can write a method for the conversion between two data structures.

Razor
<DxGrid Data="@convertedData"> <Columns> @foreach (DataColumn col in convertedData.Columns) { <DxGridDataColumn FieldName='@($"{col.ColumnName}.Data")'></DxGridDataColumn> } </Columns> </DxGrid> @code{ List<DatasetData> ColData1; List<DatasetData> ColData2; List<List<DatasetData>> WholeData; DataTable convertedData; public class DatasetData { public string Name { get; set; } public int Index { get; set; } public string Time { get; set; } public object Data { get; set; } } private DataTable ConvertFromList(List<List<DatasetData>> data){ var table = new DataTable(); for(int j = 0; j < data.Count; j++){ table.Columns.Add($"Data_{j}", typeof(DatasetData)); } int i = 0; while (i < data[0].Count){ DataRow row = table.NewRow(); for (int j = 0; j < data.Count; j++) { row[$"Data_{j}"] = data[j][i]; } table.Rows.Add(row); i++; } return table; } protected override void OnInitialized() { base.OnInitialized(); ColData1 = new List<DatasetData>() { new DatasetData(){ Data = 1, Index = 0, Name = "A", Time = ""}, new DatasetData(){ Data = 2, Index = 1, Name = "B", Time = ""}, }; ColData2 = new List<DatasetData>() { new DatasetData(){ Data = 3, Index = 0, Name = "C", Time = ""}, new DatasetData(){ Data = 4, Index = 1, Name = "D", Time = ""}, }; WholeData = new List<List<DatasetData>>() { ColData1, ColData2 }; convertedData = ConvertFromList(WholeData); } }

Please let me know if this helps.

    Comments (2)

      Thanks for your help.

      I've solved my problem with DataTable.

      C#
      private DataTable ConvertToDataTable(List<List<DatasetData>> nodeData) { var dt = new DataTable(); int RowCnt, ColCnt; if ( 0 == nodeData.Count ) return dt; RowCnt = nodeData.Count; if ( 0 == nodeData[0].Count ) return dt; ColCnt = nodeData[0].Count; List<string> ColName = new List<string>(); if(nodeData != null && nodeData[0] != null) { for(int i = 0; i < RowCnt; i++) { ColName.Add(nodeData[i][0].Name); } } for(int j = 0; j < RowCnt; j++) { dt.Columns.Add($"{ColName[j]}", typeof(DatasetData)); } for(int i = 0; i < ColCnt; i++) { DataRow row = dt.NewRow(); for(int j = 0; j < RowCnt; j++) { row[$"{ColName[j]}"] = nodeData[j][i]; } dt.Rows.Add(row); } return dt; }
      Razor
      <DxGrid data="@DisplayData"> <Columns> @foreach (DataColumn col in DisplayData.Columns) { <DxGridDataColumn FieldName='@($"{col.ColumnName}.Data")' Caption='@($"{col.ColumnName}")'> </DxGridDataColumn> } </Columns> </DxGrid>

      Thanks a lot

      Tim (DevExpress Support) 4 days ago

        Hi,

        You're welcome. Should you have further questions, feel free to ask them.

        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.