DataGrid中的0列当我使用DefaultView时

时间:2017-05-25 15:52:50

标签: c# wpf datagrid

您好我试图从DefaultView隐藏DataGrid中的列,但它给了我0列。

代码:

productsTableAdapter pta = new productsTableAdapter();
pta.Fill(dataset.products);
productsDataTable pdta = pta.GetDataByInvoiceID(invoiceID);
InvoiceProductsDataGrid.ItemsSource = pdta.DefaultView;

InvoiceProductsDataGrid.Columns[0].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[4].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[5].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[6].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[7].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[8].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[10].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[11].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[12].Visibility = Visibility.Hidden;
InvoiceProductsDataGrid.Columns[13].Visibility = Visibility.Hidden;

2 个答案:

答案 0 :(得分:0)

正如@trapspring所指出的那样,pdta听起来像是空的。在这种情况下,这样的事情可以派上用场。

int[] colsToHide = { 0, 4, 5, 6, 7, 8, 10, 11, 12, 13 };
int pointer = 0;

//Hide for as long as the desired column index exists
//Which means the order you put the indices in colsToHide
//DOES affect the results of the while loop.
while (colsToHide[pointer] < InvoiceProductsDataGrid.Columns.Count)
{
    InvoiceProductsDataGrid.Columns[colsToHide[pointer]].Visibility = Visibility.Hidden;
}

答案 1 :(得分:0)

所以答案是,我试图隐藏我要删除的东西,代码就是这个

productsTableAdapter pta = new productsTableAdapter();
pta.Fill(dataset.products);
productsDataTable pdta = pta.GetDataByInvoiceID(invoiceID);

pdta.Columns.Remove("client_id");
pdta.Columns.Remove("id1");
pdta.Columns.Remove("units");
pdta.Columns.Remove("provider");
pdta.Columns.Remove("sub_categorie");
pdta.Columns.Remove("id_invoice");
pdta.Columns.Remove("id_product");
pdta.Columns.Remove("invoice_date");
pdta.Columns.Remove("collection_method");
InvoiceProductsDataGrid.ItemsSource = pdta.DefaultView;