Question : How do I access a combo box in  a WPF datagridtemplatecolumn  using codebehind  c#

I am trying to access a combo box in a datagridtemplate. If just place the control onto the grid itself, I have no problems, but when I put the combo box in the datagrid, I cannot access it in the codebehind with the intellisense. The combo box cb_Icao is not available.

I am trying to access the combo box in a WPF UserControl in Winforms.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
<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();
        }
    }

Answer : How do I access a combo box in  a WPF datagridtemplatecolumn  using codebehind  c#

Random Solutions  
 
programming4us programming4us