0

さて、私はこのように見えるものを持っています:

会社名-1234

この場合、ダッシュの後に数字を付ける必要がないため、ユーザーが選択できるように名前をコンボボックスに含めるだけにします。

これが、Linqを使用してデータベースにクエリを実行し、ダッシュの後のすべてを削除するコードですが、コンボボックスに何かが入力されているようには見えません。

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

            var getCompanies = (from c in ctx.companyinformations
                               select c.name).ToList();

            foreach (var n in getCompanies)
            {
                compInf.Add(new companyinformation() { name = Remove(n.LastIndexOf('-')) });
            }

            cbModClient.DataSource = compInf;
            cbModClient.DisplayMember = "name";
            cbModClient.SelectedIndex = -1;
            };

このコードを試したところ、正しく機能しました。今回は「-」の代わりに「-」を使用したためだと思います。

        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;
        };
4

2 に答える 2

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";

DataSource割り当てにブレークポイントを設定することを検討し、変数が実際に期待する値を持っていることを確認/確認してください。これにより、問題がLINQに関連するのか、データバインディングに関連するのかが決まります。

于 2012-05-04T18:30:49.897 に答える
0

「まだアイテムが表示されない」という問題を再現できません。これが機能する同等のプログラムです。

データベースから結果を取得し、例外がスローされていないと仮定すると、残っている質問は次のとおりです。ComboBoxはどのように異なりますか?

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());
    }
}
于 2012-05-04T18:56:25.383 に答える