Search code examples
c#wpfdatatabledatagriditemsource

Error messege "The source contains no DataRows" when i sent a data table object to a pagenation Class method


I have a User Control with a DataGrid.Every thing was fine since the DataGride Pagenation Function within the User Control Class.I Desided to move the pagenation functions into a class instead.This is my Code

    {


    private static DataTable _dt ;
    private static TextBlock _PageInformation;
    private static ComboBox _NumberOfRecords;
    private static Button _btnPrev;
    private static Button _BtnFirst;
    private static Button _BtnNext;
    private static Button _BtnLast;
    internal static AssetsManagement equipmentManage
    {
        get { return equipmentMng; }
        set { equipmentMng = value; }
    }
    private static DataGrid _datagridObject;

    public static TextBlock PageInformation { get => _PageInformation; set => _PageInformation = value; }
    public static DataTable dt { get => _dt; set => _dt = value; }
    public static ComboBox NumberOfRecords { get => _NumberOfRecords; set => _NumberOfRecords = value; }
    public static DataGrid DatagridObject { get => _datagridObject; set => _datagridObject = value; }
    public static Button BtnPrev { get => _btnPrev; set => _btnPrev = value; }
    public static Button BtnFirst { get => _BtnFirst; set => _BtnFirst = value; }
    public static Button BtnNext { get => _BtnNext; set => _BtnNext = value; }
    public static Button BtnLast { get => _BtnLast; set => _BtnLast = value; }

    int pageIndex = 1;
    public static int numberOfRecPerPage;
    //To check the paging direction according to use selection.
    private enum PagingMode
    { First = 1, Next = 2, Previous = 3, Last = 4, PageCountChange = 5 };

    public Pagenation(DataGrid UCDatagrid,ComboBox NumberOfRecordsComboBox,TextBlock PageInfo,DataTable Data,Button Prev, Button First, Button Next, Button Last)
    {
        DatagridObject = UCDatagrid;

        dt = Data.Copy();
        if (Data == null)
        {
            MessageBox.Show(Data.Rows.Count.ToString());
        }
        PageInformation = PageInfo;
        NumberOfRecords = NumberOfRecordsComboBox;

        Prev = BtnPrev;
        First = BtnFirst;
        Next= BtnNext;
       Last= BtnLast;
        NumberOfRecordsComboBox.Items.Add("5");
        NumberOfRecordsComboBox.Items.Add("10");
        NumberOfRecordsComboBox.Items.Add("15");
        NumberOfRecordsComboBox.Items.Add("20");
        NumberOfRecordsComboBox.Items.Add("25");
        RefreshDataGrid(dt);
    }
    public  void BtnFirst_Click(object sender, RoutedEventArgs e)
    {
        Navigate((int)PagingMode.First);

    }
    public void BtnNext_Click()
    {
        Navigate((int)PagingMode.Next);

    }


    public void BtnPrev_Click(object sender, RoutedEventArgs e)
    {
        Navigate((int)PagingMode.Previous);

    }

    public void BtnLast_Click(object sender, RoutedEventArgs e)
    {
        Navigate((int)PagingMode.Last);

    }
    public void CbNumberOfRecords_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Navigate((int)PagingMode.PageCountChange);
    }

    private void Navigate(int mode)
    {
        int count;
        switch (mode)
        {
            case (int)PagingMode.Next:
                BtnPrev.IsEnabled = true;
                BtnFirst.IsEnabled = true;
                if (dt.Rows.Count >= (pageIndex * numberOfRecPerPage))
                {
                    if (dt.AsEnumerable().Skip(pageIndex *
                    numberOfRecPerPage).Take(numberOfRecPerPage).Count() == 0)
                    {
                        DatagridObject.ItemsSource = null;
                        DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Skip((pageIndex *
                        numberOfRecPerPage) - numberOfRecPerPage).Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
                        count = (pageIndex * numberOfRecPerPage) +
                        (dt.AsEnumerable().Skip(pageIndex *
                        numberOfRecPerPage).Take(numberOfRecPerPage)).Count();
                    }
                    else
                    {
                        DatagridObject.ItemsSource = null;
                        DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Skip(pageIndex *
                        numberOfRecPerPage).Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
                        count = (pageIndex * numberOfRecPerPage) +
                        (dt.AsEnumerable().Skip(pageIndex * numberOfRecPerPage).Take(numberOfRecPerPage)).Count();
                        pageIndex++;
                    }

                    PageInformation.Text = count + " of " + dt.Rows.Count;
                }

                else
                {
                    BtnNext.IsEnabled = false;
                    BtnLast.IsEnabled = false;
                }

                break;
            case (int)PagingMode.Previous:
                BtnNext.IsEnabled = true;
                BtnLast.IsEnabled = true;
                if (pageIndex > 1)
                {
                    pageIndex -= 1;
                    DatagridObject.ItemsSource = null;
                    if (pageIndex == 1)
                    {
                        DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
                        count = dt.AsEnumerable().Take(numberOfRecPerPage).Count();
                        PageInformation.Text = count + " of " + dt.Rows.Count;
                    }
                    else
                    {
                        DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Skip
                        (pageIndex * numberOfRecPerPage).Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
                        count = Math.Min(pageIndex * numberOfRecPerPage, dt.Rows.Count);
                        PageInformation.Text = count + " of " + dt.Rows.Count;

                    }
                }
                else
                {
                    BtnPrev.IsEnabled = false;
                    BtnFirst.IsEnabled = false;
                }
                break;

            case (int)PagingMode.First:
                pageIndex = 2;
                Navigate((int)PagingMode.Previous);
                break;
            case (int)PagingMode.Last:
                pageIndex = (dt.Rows.Count / numberOfRecPerPage);
                Navigate((int)PagingMode.Next);
                break;

            case (int)PagingMode.PageCountChange:
                numberOfRecPerPage = Convert.ToInt32(NumberOfRecords.SelectedItem);
                DatagridObject.ItemsSource = null;
                DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
                count = (dt.AsEnumerable().Take(numberOfRecPerPage)).Count();
                PageInformation.Text = count + " of " + dt.Rows.Count;
                BtnNext.IsEnabled = true;
                BtnLast.IsEnabled = true;
                BtnPrev.IsEnabled = true;
                BtnFirst.IsEnabled = true;
                break;
        }
    }
    public static void RefreshDataGrid(DataTable dte)
    {
        MessageBox.Show(dte.Rows.Count.ToString());

        dt = dte.Copy();
        MessageBox.Show(dt.Rows.Count.ToString());

        DatagridObject.ItemsSource = null;
        DatagridObject.ItemsSource = dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;

        int count = dt.AsEnumerable().Take(numberOfRecPerPage).Count();
        PageInformation.Text = count + " of " + dt.Rows.Count;

    }

}}

and in the Class of the UserControl I instantaite an object of the Pagenation Class with the constructor that takes all the needed varaible for pagenation.

 public PartsUC()
    {
        InitializeComponent();
        PartsDataTable = PartMng.GetAllEquipments();
        MessageBox.Show(PartsDataTable.Rows.Count.ToString());
        PageInformation = lblpageInformation;
         Partsdatagrid = this.PartsDataGrid;
        CbNumberOfRecords = this.cbNumberOfRecords;//the interface XAMAL Combobox

       Pagenation pagenation = new Pagenation(Partsdatagrid, cbNumberOfRecords, PageInformation, PartsDataTable, btnPrev, btnFirst, btnNext, btnLast);
        //Pagenation.RefreshDataGrid(PartsDataTable);}

The problem is that i still getting this error message!

The source contains no DataRows and the error points to the refreshDataGrid method in this line

DatagridObject.ItemsSource = dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;

Note that the messege box in the function refreshDataGrid shows that i have 50 rows.


Solution

  • Solved it,The thing that caused the error was the function Take() since it takes the number from the combobox that let you choose how many rows to display.I put Fixed number and It Worked.

    DatagridObject.ItemsSource = dt.AsEnumerable().Take(15).CopyToDataTable().DefaultView;