당신이 말하는 두 가지 문제점이 있습니다. 처음에는 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
'columnList'의 출처는 무엇입니까? 그 중 하나가 null이거나 비어 있다고 추측하고 있습니다. –
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 개의 문자열을 가지고 있는지 확인하기에 충분했기 때문에 심지어 테스트했습니다 .. –