2017-05-25 40 views
-2

你好我試圖隱藏從默認視圖一個DataGrid列,但它給我0列。0列在DataGrid中,當我使用默認視圖

代碼:

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; 
+1

確定PDTA將返回一個或多個行? – trapspring

+0

是的,它給的我的數據 – Dokman

回答

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; 
} 
+0

圖片鏈接什麼是展示http://imgur.com/a/mGjOq – Dokman

+0

我需要隱藏的關係,但我不能在默認視圖返回我0列在「InvoiceProductsDataGrid.ItemsSource」 – Dokman

+0

的問題是,我返回一個數據視圖,我不知道如何從一個數據視圖,並從DataRowView的隱藏列 – Dokman

0

因此,答案是,我試圖隱藏我havd刪除的東西和代碼是這樣的

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;