<dg:DataGrid x:Name="AirportDataGrid" AutoGenerateColumns="False" Loaded="OnLoad">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding="{Binding}" Header="ICAO" Width="75"/>
<dg:DataGridTemplateColumn Header="ICAO" Width="75">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ICAO}"/>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
<dg:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Height="22"
ItemsSource="{Binding}"
Name="cb_Icao"
SelectedIndex="0"/> </DataTemplate>
</dg:DataGridTemplateColumn.CellEditingTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
Codebehind:
private void ListAirports()
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlCon;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Airport";
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = cmd;
DataSet ds = new DataSet();
try
{
sqlDa.Fill(ds, "Airport");
//Binding the data to the combobox.
cb_Icao.DataContext = ds.Tables["Airport"].DefaultView;
//To display category name
cb_Icao.DisplayMemberPath = ds.Tables["Airport"].Columns["ICAO"].ToString();
//To store the ID as hidden
cb_Icao.SelectedValuePath = ds.Tables["Airport"].Columns["ICAO"].ToString();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while loading airport.");
}
finally
{
sqlDa.Dispose();
cmd.Dispose();
sqlCon.Dispose();
}
}
|