4

내 프로젝트는 Excel 시트의 데이터를 데이터베이스로 가져 와서 수동으로 매핑하도록 사용자에게 요청하는 C# 응용 프로그램입니다.C# 2 행 작성, 첫 번째 텍스트보기 목록 및 두 번째 콤 상자 목록입니다

나는 두 행을 작성해야합니다 :

  1. 첫 번째 행은 엑셀의 헤더 적합에 DataGridTextColumn입니다.
  2. 번째 행은 DataGridTextColumn 각자 콤보 박스가

데이터베이스의 COLUMN_NAME 갖는 DataGridComboBoxColumn이다. 나는 DataGridComboBoxColumn 작업, 내가 응용 프로그램을 실행할 때마다 만들 수 아니에요

하지만이 문제에 직면하고있어는, 첫 번째 행은 정상적으로 작동하지만 두 번째 행은 비어

코드 :

foreach (DataRow row in dt.Rows) 
{ 
    DataGridTextColumn dgtc = new DataGridTextColumn(); 
    dgtc.MinWidth = 100; 
    dgtc.CanUserSort = false; 
    dgtc.Header = row["Column_name"].ToString(); 
    dg.Columns.Add(dgt); 
    DataGridComboBoxColumn dgcbc = new DataGridComboBoxColumn(); 
    dgcbc.ItemsSource = columnList; 
    dgcbc.MinWidth = 100; 
    dg2.Columns.Add(dgcbc); 
} 

XAML :

<DataGrid x:Name="dg" HorizontalAlignment="Left" Height="29" Margin="11,72,0,0" VerticalAlignment="Top" Width="579"/> 
<DataGrid x:Name="dg2" HorizontalAlignment="Left" Height="30" Margin="11,106,0,0" VerticalAlignment="Top" Width="579"/> 

라이브 뷰 :

screenshot

코드는 작동하지만 콤보 상자는 항상 빈 필드를 표시합니다.

DataGridComboBoxColumn이 작동하지 않는 이유에 대한 도움이 필요합니까?

+0

'columnList'의 출처는 무엇입니까? 그 중 하나가 null이거나 비어 있다고 추측하고 있습니다. –

+0

SqlConnection conn = 새 SqlConnection ("server = ***; database = ***; Integrated Security = true"); string [] restrictions = new string [4] {null, null, "orders", null}; conn.Open(); var columnList = conn.GetSchema ("Columns", restrictions) .AsEnumerable(). Select (s => s.Field ("Column_Name")). ToList(); 그것은 columnList가 null이 아니고 4 개의 문자열을 가지고 있는지 확인하기에 충분했기 때문에 심지어 테스트했습니다 .. –

답변

1

당신이 말하는 두 가지 문제점이 있습니다. 처음에는 dgcbc.ItemsSource = columnList 설정으로 ComboBox에서 선택할 항목 목록을 표시하기에 충분하지 않습니다.

당신은 예를 들어, DisplayMemberPath 및 SelectedValuePath 속성을 설정해야합니다 columnList의 유형에 따라 :

var columnList = new Dictionary<string, string> 
{ 
    { "123", "test 123" }, 
    { "aaa", "test aaa" }, 
    { "qwe", "test qwe" } 
}; 

dgcbc.ItemsSource = columnList; 
dgcbc.DisplayMemberPath = "Key"; 
dgcbc.SelectedValuePath = "Value"; 

또 다른 문제가 DataGrid 객체 (들)에 설정된 ItemsSource에 열 결합되어있다.

dg2.ItemsSource = dt; 
dgcbc.TextBinding = new Binding(string.Format("[{0}]", "Column_Name"); 

또한 DataGridTextColumn에 텍스트를 표시하는 방법을 궁금해 수 있습니다

dgtc.Binding = new Binding(string.Format("[{0}]", "Column_Name"); 

내가 확실하지 오전이 당신이 열 매핑 무엇을 달성하고자하는 경우, 아마 당신의 헤더 템플릿을 수정하고 싶습니다 그리드를 제시하고 아래의 텍스트로 그리드 데이터를 표시하십시오. 이렇게하려면 DataGridTemplateColumn DataGridTextColumn 열과 머리글에 Label 및 ComboBox가 모두 있어야합니다. 도움이 되길 바랍니다.

편집 :

신속하고 더러운 코드 전용 솔루션을 준비했습니다.

XAML :

<DataGrid x:Name="dg" Grid.Row="0" AutoGenerateColumns="False"/> 

코드 숨김 선택이 승인 된 후

 // data is a rough equivalent of DataTable being imported 
     var data = new List<Dictionary<string, string>> 
     { 
      new Dictionary<string, string> { { "column1", "asd asfs af" }, { "column2", "45dfdsf d6" }, { "column3", "7hgj gh89" } }, 
      new Dictionary<string, string> { { "column1", "aaasdfda" }, { "column2", "45sdfdsf 6" }, { "column3", "78gh jghj9" } }, 
      new Dictionary<string, string> { { "column1", "s dfds fds f" }, { "column2", "4dsf dsf 56" }, { "column3", "78gh jgh j9" } }, 
     }; 

     // a list of columns to map to 
     var importToColumns = new List<string> 
     { 
      "123", 
      "aaa", 
      "qwe", 
      "456", 
      "bbb" 
     }; 

     importMappings = new Dictionary<string, int>(); 
     foreach(var column in data[0]) 
     { 
      importMappings.Add(column.Key, -1); 
     } 

     foreach(var r in importMappings) 
     { 
      var dgtc = new DataGridTextColumn(); 
      dgtc.Binding = new Binding(string.Format("[{0}]", r.Key)); 
      var sp = new StackPanel(); 
      dgtc.Header = sp; 
      sp.Children.Add(new Label { Content = r.Key }); 
      var combo = new ComboBox(); 
      sp.Children.Add(combo); 
      combo.ItemsSource = importToColumns; 
      var selectedBinding = new Binding(string.Format("[{0}]", r.Key)); 
      selectedBinding.Source = importMappings; 
      combo.SetBinding(Selector.SelectedIndexProperty, selectedBinding); 
      dgtc.MinWidth = 100; 
      dgtc.CanUserSort = false; 
      dg.Columns.Add(dgtc); 
     } 

     dg.ItemsSource = data; 
    } 

    private Dictionary<string, int> importMappings; 

이 importMappings 컬럼의 매핑의 목록이 포함됩니다 - 그것은의 인덱스를 포함 각 가져 오기 파일 열의를 importToColumns리스트의 요소. 요소가 선택되어 있지 않은 경우는 -1

+0

이제 덕분에 그 질문에 대한 답을 얻을 수 있습니다 .. 헤더 레이블과 ComboBox를 모두 사용하여 DataGridTemplateColumn 열을 구현하는 방법 그 안에. 불행히도 3 일간의 연구가 있었지만 아직은 아무 것도 ... –

+0

코드에 몇 가지 구성을했지만 감사를 많이 받았다. –