<Window x:Class="Kvik_Support.KonsulentValg"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="KonsulentValg" Height="150" Width="250"
Background="LightGray" ResizeMode="NoResize">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="10" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="10*" />
<RowDefinition Height="10*" />
<RowDefinition Height="10*" />
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<Label Content="Hvem er du?" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
<ComboBox Grid.Column="1" Grid.Row="2" Height="25" Name="cmbHvemErDu">
</ComboBox>
<Button Grid.Column="1" Grid.Row="3" Height="25" Width="100" Content="OK" Click="Button_Click" />
</Grid>
</Window>
这是我的窗口的 XAML 代码,我正在尝试用数据库中的项目填充组合框......
我的想法是我可以用以下代码填写项目,但这会导致 XAML 解析异常......
public partial class KonsulentValg : Window
{
public KonsulentValg()
{
InitializeComponent();
try
{
SqlConnection lager = new SqlConnection("Data Source=local\\SERVER;Initial Catalog=Kvik-Support;Integrated Security=True");
lager.Open();
SqlCommand thisCommand = lager.CreateCommand();
string command = "SELECT ID, navn FROM Konsulent";
SqlDataAdapter da = new SqlDataAdapter(command, lager);
SqlDataReader thisReader = thisCommand.ExecuteReader();
DataTable datatable = new DataTable();
da.Fill(datatable);
foreach (DataRow row in datatable.Rows)
{
string rows = string.Format("{0}:{1}", row.ItemArray[0], row.ItemArray[1]);
cmbHvemErDu.Items.Add(rows);
}
Console.ReadKey();
thisReader.Close();
lager.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}
但它可能没有工作......