如何从我的List <t> </t>获取数据

时间:2012-11-16 02:10:01

标签: c# wpf

guyz我知道如何从我的列表中添加数据,但问题是如何检索它??

id在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

1 个答案:

答案 0 :(得分: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
}