2012-05-04 7 views
0

オーケーをフォーマットした後、私はこのようなもの持っている多くの名前とCOMBOXを移入:私は名前だけのためにユーザが選択するためのコンボボックスになりたい彼らに

会社名-1234

をダッシュの後に数字を付けることは、この場合に必要ではありません。

私のコードは、Linqでデータベースにクエリを行い、その後にダッシュの後にすべてを削除しますが、それは何もせずにコンボボックスを投入しているようです。 「 - 」の代わりに私はその私が使用しているためだと思い、

​​

は、私はこのコードを試してみました、それが働いたperfrectly「 - 」この時間を。

 using (context ctx = new context()) 
     { 
      List<companyinformation> compInf = new List<companyinformation>(ctx.companyinformations); 

      var getCompanies = (from c in compInf 
         where c.name.Contains("-") 
         select c.name.Substring(0, c.name.LastIndexOf("-"))).ToList(); 

      cbModClient.DataSource = getCompanies; 
      cbModClient.DisplayMember = "name"; 
      cbModClient.SelectedIndex = -1; 
     }; 
+0

コンボボックスのデータソースは、新しく作成されたリストではなく元のデータに設定します。おそらくより記述的な変数名を使用した場合、このような間違いは少なくなります;) – Tergiver

+0

まあ、私はmaynものを変更しています。これは単純なタイプミスだと思うことがありますが、私はCompInfに変更しました。何も入れません! – MDL

+1

クエリがデータを返すのは確実ですか? – Tergiver

答えて

2

あなたはgetCompanies結果コレクションに結合している、しかし、あなたは、文字列操作を行い、compInfにそれらを追加しました。

cbModClient.DataSource = compInf; 

おそらく短い方法:

var companyNames = ctx.companyinformations 
         .Select(c=> new {FormattedName = 
           c.name.Substring(0,c.name.LastIndexOf('-')) 
             .Trim()}) 
         .ToList(); 

cbModClient.DataSource = companyNames; 
cbModClient.DisplayMember = "FormattedName"; 
cbModClient.ValueMember = "FormattedName"; 

はあなたの変数が実際にあなたが期待値を持っていることを確認/データソースの割り当てにブレークポイントを置くことを検討し、検査します。それはあなたの問題がLINQに関連しているのか、それともデータバインドに関連しているのかを判断します。

+0

+1名前をインラインに変換する方がスリムです。 – Tergiver

+0

私はこれを試してもまだ運がありません:(コンボボックスにはまだ何も表示されていません) – MDL

+0

ありがとう、.Removeの代わりに.Substringを使用すると、これはきれいになりました! – MDL

0

「まだアイテムが表示されません」という問題を複製できません。ここには動作する同等のプログラムがあります。

データベースから結果が得られ、例外がスローされないと仮定すると、残りの質問は次のようになります。コンボボックスはどのように異なっていますか?

using System; 
using System.Drawing; 
using System.Linq; 
using System.Windows.Forms; 

class Form1 : Form 
{ 
    class CompanyInfo 
    { 
     public string Name { get; set; } 
    } 

    static string RemoveTrailingDash(string value) 
    { 
     int dashIndex = value.LastIndexOf('-'); 
     if (dashIndex > 0) 
      return value.Substring(0, dashIndex).TrimEnd(); 
     return value; 
    } 

    public Form1() 
    { 
     var companies = new CompanyInfo[] 
     { 
      new CompanyInfo { Name = "Ajax - 1200" }, 
      new CompanyInfo { Name = "Bermuda Corp - 1" }, 
      new CompanyInfo { Name = "Capitol Inc" }, 
      new CompanyInfo { Name = "Dash LLC - " }, 
     }; 

     Controls.Add(new ComboBox 
     { 
      Location = new Point(10, 10), 
      DropDownStyle = ComboBoxStyle.DropDownList, 

      DataSource = companies.Select(c => new { FormattedName = RemoveTrailingDash(c.Name) }).ToList(), 
      DisplayMember = "FormattedName", 
     }); 
    } 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 
} 
+0

@MDL – Tergiver

+0

努力してくれてありがとう、何かの理由で、私が " - "の代わりに " - "を使ったときにうまくいきました。 – MDL

関連する問題