2012-05-04 3 views
0

좋아 I는 다음과 같습니다 뭔가가 있습니다를 포맷 한 후 많은 이름을 가진 combox 채우기

가 난 단지 사용자가 있기 때문에 선택의 이름은 콤보 상자에 있어야 할

회사 명-1234 이 경우 대시와 그 뒤에 오는 숫자는 꼭 필요한 것은 아닙니다.

Heres는 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; 
     }; 
+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

"아직 나타나는 항목이 없습니다"라는 문제를 복제 할 수 없습니다. 다음은 동등한 프로그램입니다.

데이터베이스에서 결과를 얻고 예외가 발생하지 않는다고 가정하면 남은 질문은 다음과 같습니다. 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()); 
    } 
} 
+0

@MDL @MDL이 데이터를보다 자세히 반영하기 위해 편집했습니다. – Tergiver

+0

노력에 감사드립니다. 어떤 이유로 "-"대신 "-"를 사용했을 때 작동했습니다. – MDL

관련 문제