1

I have written a program in C# that searches a database for specific tools and shows the specs for that tool in a DataGrid. I have been asked to change one field in the DataGrid to a ComboBox. I think I am almost there but I can't seem to figure out how to put it in the column I want. Any help would be appreciated.

WPF Code `

        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="ComboBox" x:Name="comboboxColumn1"
            SelectedValueBinding="{Binding ComboBox}" />

        </DataGrid.Columns>

    </DataGrid>

`

C# code `

public partial class Termination : Window
{

    DataTable Lds;
    SqlDataAdapter adapter;
    SqlConnection connection;

    public List<string> GVComboBox { get; set; }

    public Termination()
    {
        InitializeComponent();
        GVComboBox = new List<string>() { "CRV Owned", "Customer Owned", "CRV Leased", "Customer Leased", "" };
        comboboxColumn1.ItemsSource = GVComboBox;


    }

    private void ApplicatorB_Click(object sender, RoutedEventArgs e)
    {

        Common.sqlApplicator = "Select Terminal,Applicator,applicator_Type as 'Applicator Type',Hand_tool as 'Hand Tool', Hand_Tool_Type as 'Hand Tool Type',Wire_Gauge as 'Wire Gauge',Crimp_Height as 'Core Crimp Height',Conductor_Crimp_Width as 'Core Crimp Width',Insulator_Crimp_Height as 'Insulation Crimp Height',Insulator_Crimp_Width as 'Insulation Crimp Width',Tooling,Notes,Strip_Length as 'Strip Length',Applicator_Status as 'Applicator Status' ,ID from Tooling_Specs where Applicator like '" + ApplicatorTB.Text + "%' order by Applicator";

        try
        { 
            connection.Open();
            adapter = new SqlDataAdapter(Common.sqlApplicator, connection);
            Lds = new DataTable("Applicator");
            adapter.Fill(Lds);
            OutputDG.ItemsSource = Lds.DefaultView;

        }
    catch (Exception E)
            {
            MessageBox.Show(E.ToString());
            }          
    finally
            {
            connection.Close();
    };
        Common.Applicator = ApplicatorTB.Text;
        Common.Terminal = TerminalTB.Text;
        Common.Tool = ToolTB.Text;

        if (Common.admin == false)
        {
            ApplicatorTB.Clear();
        }          
    }`
Taterhead
  • 5,763
  • 4
  • 31
  • 40
TEC C
  • 153
  • 1
  • 3
  • 18
  • Try this: http://stackoverflow.com/questions/19003133/wpf-datagrid-combobox-column – Richard Mar 15 '16 at 16:41
  • Add the code above ` ` and try to remove extra code that is not relevant to this question, it would help others help you. – Shashank Shekhar Mar 15 '16 at 16:48
  • Richard thanks for the reply but I am already past that point. What I am trying to do is add a combobox to data that is already being pulled from a database. I have looked at the link you included and it is pretty much what I currently have in my code. – TEC C Mar 15 '16 at 16:49
  • You have SelectedValueBinding="{Binding ComboBox}" but I don't see what that is bound to in your C# code. Does the combo box show the list of options you define in GVComboBox? – Jay T Mar 15 '16 at 17:16
  • you would need rows in the grid for the combobox to show up, are there other columns in the datagrid? I assume that the itemsource for the combobox will be property of an object, the list of which is the itemsource for the datagrid – Shashank Shekhar Mar 15 '16 at 17:27
  • Jay it is bound to GVComboBox and it does work. – TEC C Mar 15 '16 at 17:51
  • Shedhar the the combobox i currently have does show up because I am inserting rows from the database the column headers are acquired from the output of the select statement. the column I want the combobox applied to is "Applicator Status" – TEC C Mar 15 '16 at 17:57
  • The SelectedValueBinding property should be bound to the corresponding property for each object in the DataTable. In addition, the ItemsSource property should be bound to the list of strings you define in the constructor. I can't see from the code you posted what the binding paths should be because you haven't included the type of each data object in Lds.DefaultView. – Jay T Mar 15 '16 at 18:52

0 Answers0