当我将数据 table 对象发送到分页 Class 方法时出现错误消息 "The source contains no DataRows"
Error messege "The source contains no DataRows" when i sent a data table object to a pagenation Class method
我有一个带有 DataGrid.Every 的用户控件很好,因为用户控件中的 DataGride 分页功能 Class.I 决定将分页功能移动到 class instead.This 是我的代码
{
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;
}
}}
并且在 UserControl 的 Class 中,我使用构造函数实例化了一个分页 Class 的对象,该构造函数采用了分页所需的所有变量。
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 source contains no DataRows
and the error points to the refreshDataGrid method in this line
DatagridObject.ItemsSource = dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
请注意,函数 refreshDataGrid 中的消息框显示我有 50 行。
已解决,导致错误的是函数 Take(),因为它从组合框中获取数字,让您选择要 display.I 放置固定数字的行数,它起作用了。
DatagridObject.ItemsSource = dt.AsEnumerable().Take(15).CopyToDataTable().DefaultView;
我有一个带有 DataGrid.Every 的用户控件很好,因为用户控件中的 DataGride 分页功能 Class.I 决定将分页功能移动到 class instead.This 是我的代码
{
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;
}
}}
并且在 UserControl 的 Class 中,我使用构造函数实例化了一个分页 Class 的对象,该构造函数采用了分页所需的所有变量。
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 source contains no DataRows and the error points to the refreshDataGrid method in this line
DatagridObject.ItemsSource = dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
请注意,函数 refreshDataGrid 中的消息框显示我有 50 行。
已解决,导致错误的是函数 Take(),因为它从组合框中获取数字,让您选择要 display.I 放置固定数字的行数,它起作用了。
DatagridObject.ItemsSource = dt.AsEnumerable().Take(15).CopyToDataTable().DefaultView;