【问题标题】:Passing multiple selected value from a Listbox to an Array as parameters in C#将多个选定值从列表框传递到数组作为 C# 中的参数
【发布时间】:2014-05-23 17:38:22
【问题描述】:

下面是我的 C# 代码,用于从列表框中填充多选项目

List<string> listCountry = new List<string>();
for (int i = 0; i < lstCountry.Items.Count; i++)
{
   if (lstCountry.Items[i]Selected)
   {
       countries = listCountry.Add(lstCountry.Items[i].ToString());
    }
}     

而且我有一行调用上述参数运行报表的方法:

retVal = CR.GetReport(Company, countries);

我的问题是:我应该为 countries 定义什么数据类型,因为当我将国家定义为 字符串国家=空; 我在这里做错了什么?请帮忙,非常感谢

对不起,我说的不够清楚,我还有另一个函数 GetReport() 定义为

public CrystalDecisions.CrystalReports.Engine.ReportDocument GetReport( string      Company,  string countries)
{
    CrystalDecisions.CrystalReports.Engine.ReportDocument retVal = new rptReortData();
     ReportLogon rptLog = new ReportLogon();
     rptLog.logon(retVal, "Report");
     retVal.SetParameterValue("P_Country", new string[] { country});
}  

如何从分配给国家的列表框中获取值

【问题讨论】:

    标签: c# listbox


    【解决方案1】:

    您没有提供函数的名称,但我猜它是 GetReport。它不返回任何值,因此您无法分配 retVal。试试下面的:

    CR.GetReport(Company, countries);
    

    【讨论】:

    • 我添加了函数GetReport()。我在那里做错了什么?
    【解决方案2】:

    我对你的问题有点疑惑,但我猜 CR.GetReport 函数引发了异常?因此,国家/地区的数据类型取决于该函数。

    我可能会做出以下改变:

    listCountry.Add((lstCountry.Items[i] == null ? string.Empty : lstCountry.Items[i].ToString()));
    

    【讨论】:

      【解决方案3】:
      List<string> listText = new List<string>();
      List<string> listValue = new List<string>();
      foreach (int index in ListBox1.GetSelectedIndices()) {
          listText.Add(ListBox1.Items[index].Text);
          listValue.Add(ListBox1.Items[index].Value);
      }
      

      【讨论】:

        【解决方案4】:

        你需要从你的函数中返回 retVal

        public CrystalDecisions.CrystalReports.Engine.ReportDocument GetReport( string      Company,  string countries)
        {
            CrystalDecisions.CrystalReports.Engine.ReportDocument retVal = new rptResearchDataDownload();
             ReportLogon rptLog = new ReportLogon();
             rptLog.logon(retVal, "Report");
             retVal.SetParameterValue("P_Country", new string[] { country});
        
            // ADD THIS LINE
            return retVal;
        
        }  
        

        您还需要将列表转换为字符串。你可以这样做:

        countries = listCountry.Aggregate((list, c) => list + c + ",");
        

        【讨论】:

        • 是的,我补充说,这部分效果很好,非常感谢。但是list的数据类型与国家冲突。
        • 嗨 Bartosz,list 是什么,在我输入 Aggregate 后,它没有显示在我的 VS2010 智能中
        • list 是 lambda 表达式的参数名称。你可以把它命名为任何你喜欢的名字。 'l' 的简称。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多