2012-11-16 50 views
0

guyz我知道如何從我的列表中添加數據,但問題是如何檢索它...?如何從我的列表中獲取數據<T>

ID delcared我在GlobalVar.cs名單:

public static List<string> ViolationRefNumToPrint = new List<string>(); 

這裏的代碼後面添加數據到我的清單.....

GlobalVar.ViolationRefNumToPrint.Clear(); 
    for (int i = 0; i < lvviolations.Items.Count; i++) 
    { 
     GlobalVar.ViolationRefNumToPrint.Add(((EmpViolationObject)lvviolations.Items[i]).VioRefNum); 
    } 

我的問題是我怎麼能檢索我的列表... :(

編輯

guyz我已經使用下面的代碼。這是由@evanmcdonnal給出的。其實我要去使用這個對我的報告...我已經使用的DocumentViewer

這裏是我的代碼....

 ReportDocument reportDocument = new ReportDocument(); 

     string ats = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName; 

     StreamReader reader = new StreamReader(new FileStream(ats.ToString() + @"\Template\ReportViolation.xaml", FileMode.Open, FileAccess.Read)); 

     reportDocument.XamlData = reader.ReadToEnd(); 
     reportDocument.XamlImagePath = Path.Combine(ats.ToString(), @"Template\"); 
     reader.Close(); 


     DateTime dateTimeStart = DateTime.Now; // start time measure here 
     List<ReportData> listData = new List<ReportData>(); 
     int i = 0; 

     foreach (string item in GlobalVar.ViolationRefNumToPrint) 
     { 
      ReportData data = new ReportData(); 

      data.ReportDocumentValues.Add("PrintDate", DateTime.Now); 
      data.ReportDocumentValues.Add("EmpIDNum", NewIDNumber.ToString()); 
      data.ReportDocumentValues.Add("EmpName", NewEmpName.ToString()); 
      data.ReportDocumentValues.Add("EmpPosition", NewPosition.ToString()); 

      data.ReportDocumentValues.Add("PageNumber",(i + 1)); 
      data.ReportDocumentValues.Add("PageCount", GlobalVar.ViolationRefNumToPrint.Count.ToString()); 

      listData.Add(data); 
      i++; 
     } 

     XpsDocument xps = reportDocument.CreateXpsDocument(listData); 
     documentViewer.Document = xps.GetFixedDocumentSequence(); 

     // show the elapsed time in window title 
     Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms"; 

這裏的問題是,它給的我的錯誤是這樣....

enter image description here

+0

找回什麼到你的列表。如果你的意思是訪問的項目?在你的列表中,只需遍歷它 – Bernard

+0

檢索它做什麼?顯示值? – fsimonazzi

+1

'ViolationRefNumToPrint [listIndex];'或迭代它:'foreach(var vioRefNum in ViolationRefNumToPrint){/ }' –

回答

0

你必須遍歷並搜索您想要的項目。

foreach (string item in ViolationRefNumToPrint) 
{ 
     Console.Out(item); 
} 

相反,如果你想有一個特定項目(假設您的列表中有對象與條件叫它string itemImLookinFor = "some nonsense";循環遍歷它來搭配;

foreach (MyObject item in ViolationRefNumToPrint) 
{ 
     if (item.name == itemImLookinFor) 
      //do something with this object 
}