Why does my Xamarin.Forms.DataGrid not displaying any data?

Fourie Laing 1 Reputation point
2022-01-20T09:02:37.847+00:00

Hi everyone,

My datagrid is not displaying any data in my datagrid
I belief it is a binding issue which I'm overlooking.

Here is my xaml code:

<ContentView>
                    <ScrollView Orientation="Horizontal">
                        <dg:DataGrid x:Name="dgvItemDetailsGrid" ItemsSource="{Binding BuildItemDetailsData}"
                                     RowHeight="40"
                                     HeaderHeight="50" HeaderBackground="#E0E6f8"
                                     HeaderFontSize="15" ActiveRowColor="#8899AA">
                            <!--SelectedItem="{Binding ItemDetailsSelected}"-->

                            <x:Arguments>
                                <ListViewCachingStrategy>RetainElement</ListViewCachingStrategy>
                            </x:Arguments>
                            <dg:DataGrid.Columns>
                                <dg:DataGridColumn Title="Doc Entry" PropertyName="DocEntryItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Doc Num" PropertyName="DocNumItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Item Number" PropertyName="ItemCodeItemDetails" Width="150" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Open Quantity" PropertyName="OpenQtyItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Unit of Measurement" PropertyName="UomCodeItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Order Date" PropertyName="DocDateItemDetails" Width="150" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Order Time" PropertyName="U_DRTItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Weight" PropertyName="Weight1ItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Original Order Quantity" PropertyName="QuantityItemDetails" 
                                                   Width="125" HorizontalContentAlignment="Center" 
                                                   VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Price" PropertyName="PriceItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                <dg:DataGridColumn Title="Packing" PropertyName="U_PACItemDetails" Width="125" 
                                               HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                            </dg:DataGrid.Columns>
                            <dg:DataGrid.RowsBackgroundColorPalette>
                                <dg:PaletteCollection>
                                    <Color>#E0E6f8</Color>
                                </dg:PaletteCollection>
                            </dg:DataGrid.RowsBackgroundColorPalette>
                        </dg:DataGrid>
                    </ScrollView>
                </ContentView>

Here is my Model Code

public class DgvItemDetails
    {
        public int DocEntryItemDetails { get; set; }
        public int DocNumItemDetails { get; set; }
        public string ItemCodeItemDetails { get; set; }
        public decimal OpenQtyItemDetails { get; set; }
        public string UomCodeItemDetails { get; set; }
        public DateTime DocDateItemDetails { get; set; }
        public string U_DRTItemDetails { get; set; }
        public decimal Weight1ItemDetails { get; set; }
        public decimal QuantityItemDetails { get; set; }
        public decimal PriceItemDetails { get; set; }
        public string U_PACItemDetails { get; set; }
    }

Here is my ViewModel Code

public ObservableCollection<DgvItemDetails> BuildItemDetailsData { get; set; } = new ObservableCollection<DgvItemDetails>();

private void GetSQLFillOrderDetails()
        {
            string Customer = string.Empty;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                if (selectedCustomer != "")
                {
                    string[] CustomerName = selectedCustomer.Split(':');
                    Customer = CustomerName[0];
                }

                try
                {
                    string[] OrderNr;
                    OrderNr = selectedSalesOrder.Split(':');
                    string SQLCommand = @"SELECT rdl.DocEntry AS [Doc Entry], CAST(" + OrderNr[0].Trim() + " AS int) AS [Doc Number]," +
                        "rdl.ItemCode AS [Item Number], rdl.OpenQty AS [Open Quantity], rdl.UomCode as [Unit of Measurement], " +
                        "cast(rdl.DocDate AS date) AS [Order Date], ISNULL(U_DRT,'') AS [Order Time], rdl.Weight1 AS [Weight], " +
                        "rdl.Quantity AS [Original Order Quantity], " +
                        "rdl.Price AS [Price], " +
                        "ISNULL(rdl.U_PAC,'') AS [Packing] " +
                        "FROM RDR1 rdl " +
                        "INNER JOIN ORDR ord ON(ord.DocEntry = rdl.DocEntry) " +
                        "WHERE rdl.LineStatus <> 'C' AND ord.CardCode = '" +
                        Customer + "' AND ord.DocEntry = '" + DocEntrySelected + "'";

                    using (SqlCommand command = new SqlCommand(SQLCommand, connection))
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader != null)
                            {
                                while (reader.Read())
                                {
                                    BuildItemDetailsData.Add(new DgvItemDetails()
                                    {
                                        DocEntryItemDetails = reader.GetInt32(0),
                                        DocNumItemDetails = reader.GetInt32(1),
                                        ItemCodeItemDetails = reader.GetString(2),
                                        OpenQtyItemDetails = reader.GetDecimal(3),
                                        UomCodeItemDetails = reader.GetString(4),
                                        DocDateItemDetails = reader.GetDateTime(5),
                                        U_DRTItemDetails = reader.GetString(6),
                                        Weight1ItemDetails = reader.GetDecimal(7),
                                        QuantityItemDetails = reader.GetDecimal(8),
                                        PriceItemDetails = reader.GetDecimal(9),
                                        U_PACItemDetails = reader.GetString(10)
                                    });
                                }
                                Application.Current.MainPage.BindingContext = BuildItemDetailsData;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Application.Current.MainPage.DisplayAlert("Alert", "Error Creating order item list table :", ex.Message);
                }
                connection.Close();
            }
        }

Any help would be appreciated

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,231 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2022-01-21T01:58:58.593+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    This issue is related to Application.Current.MainPage.BindingContext = BuildItemDetailsData; line.

    GetSQLFillOrderDetails method in the viewmodel. Please do not bindingContext in the viewmodel. Please remove it and make sure BuildItemDetailsData have items.

    And your Xamarin.Forms.DataGrid is in a ContentView. Do you want to use this ContentView in the MainPage? If so, please remove bindingContext code to the MainPage's background code or Compiled Bindings.

    Background code.

       public partial class MainPage : ContentPage  
           {  
               public MainPage()  
               {  
                   InitializeComponent();  
         
                   BindingContext = new MyWiewModel();  
               }  
           }  
    

    Or using Compiled Bindings

       <StackLayout>  
               <StackLayout.BindingContext>  
                   <app8:MyWiewModel  />  
               </StackLayout.BindingContext>  
                 
               <app8:View1></app8:View1>  
           </StackLayout>  
    

    If you want to know more details about MVVM. There are lots of good docs on MVVM.

    MVVM is in the Xaml Basics docs (including a sample)
    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm

    And in learning modules:
    https://learn.microsoft.com/en-us/learn/modules/design-a-mvvm-viewmodel-for-xamarin-forms/

    As well as in (old, but conceptually still valid) books:
    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm

    If you still do not know how to change your code. I can give you simple code completely .

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.