Read SelectedItem from DataBound Combobox - wpf

I have a databound ComboBox that is linked to an Collection of custom objects...
Public Property printerlist As New ObservableCollection(Of Printers)
[..]
Dim PrintersList = New List(Of Printers)
'WMI Stuff
Dim objMS As System.Management.ManagementScope = New System.Management.ManagementScope(ManagementPath.DefaultPath)
objMS.Connect()
'Query Printers
Dim objquery As SelectQuery = New SelectQuery("SELECT * FROM Win32_Printer")
Dim objMOS As ManagementObjectSearcher = New ManagementObjectSearcher(objMS, objquery)
Dim objMOC As System.Management.ManagementObjectCollection = objMOS.Get()
Try
For Each Printers As ManagementObject In objMOC
If CBool(Printers("Local")) Then
PrintersList.Add(New Printers With {.DeviceName = Printers("Name"), .Type = "Local"})
End If
If CBool(Printers("Network")) Then
PrintersList.Add(New Printers With {.DeviceName = Printers("Name"), .Type = "Network"})
End If
Next
Catch ex As Exception
Debug.Print(ex.Message)
End Try
Dim LCV As ListCollectionView = New ListCollectionView(PrintersList)
Printer_Select.ItemsSource = LCV
[..]
Public Class Printers
Public Property DeviceName As String
Public Property Type As String
End Class
<ComboBox x:Name="Printer_Select" Background="{x:Null}" Padding="4,5,4,3" BorderBrush="Gainsboro" >
<ComboBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="11" FontFamily="Segoe UI Semibold"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ComboBox.GroupStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DeviceName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
When I try to retrieve the SelectedItem of the ComboBox 'Printer_Select', I either get the name of the element or the error 'Conversion from type 'Printers' to type 'String' is not valid.'
How do I get the DeviceName of the ComboBoxItem when it's selected in the dropdown?

Cast the SelectedItem to a Printers object:
Dim selectedPrinter As Printers = TryCast(Printer_Select.SelectedItem, Printers)
If selectedPrinter IsNot Nothing Then
Dim deviceName As String = selectedPrinter.DeviceName
'...
End If

Related

WPF DataGrid Column Template for custom object

I have a WPF DataGrid bound to a DataTable. The DataTable has columns containing strings and columns containing custom objects (Requirement):
public class PdfFormData
{
public string SupplierName { get; set; }
public List<Requirement> Requirements = new List<Requirement>();
public class Requirement
{
public string ID { get; set; }
public string SupplierStatus { get; set; }
public string SupplierComment { get; set; }
public string OEMStatus { get; set; }
}
}
I'm adding the columns to the DataGrid with this loop:
PHDataGrid.ItemsSource = dataSet.Tables["ReqIfTable"].DefaultView;
PHDataGrid.Columns.Clear();
foreach (DataColumn dataColumn in dataSet.Tables["ReqIfTable"].Columns)
{
if(dataColumn.DataType == typeof(ReqForms.PdfFormData.Requirement))
{
DataGridTemplateColumn dgColumn = new DataGridTemplateColumn();
dgColumn.Header = dataColumn.ColumnName;
dgColumn.CellTemplate = (DataTemplate)FindResource("dgTemplateRequirement");
dgColumn.CanUserSort = true;
dgColumn.IsReadOnly = true;
PHDataGrid.Columns.Add(dgColumn);
}
else
{
DataGridTextColumn dgColumn = new DataGridTextColumn();
dgColumn.Header = dataColumn.ColumnName;
Binding dgBinding = new Binding(dataColumn.ColumnName);
dgColumn.Binding = dgBinding;
dgColumn.CanUserSort = true;
dgColumn.IsReadOnly = true;
PHDataGrid.Columns.Add(dgColumn);
}
}
And I have defined a template for showing my Requirement objects:
<Window.Resources>
<DataTemplate x:Key="dgTemplateRequirement">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Requirement.SupplierStatus}" />
<TextBlock Text="{Binding Requirement.SupplierComment}" />
<TextBlock Text="{Binding Requirement.OEMStatus}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
The Binding to the Requirement properties (SupplierStatus, SupplierComment, ...) is not working and stays empty. How can I bind to the properties of my custom object that is stored in the DataTable?
The bindings do not work, because the DataContext of the the templated cell is a DataRowView.
You can either create a converter as in this related post or just access the members directly with an indexer binding path, where requirement is the name of your column in the DataTable, e.g.:
<TextBlock Text="{Binding Row[requirement].SupplierStatus}" />
I set the DataContext of the StackPanel to the Requirement object, so it is even easier to bind.
<DataTemplate x:Key="dgTemplateRequirement">
<StackPanel Orientation="Vertical" DataContext="{Binding Row[requirement]}">
<TextBlock Text="{Binding SupplierStatus}" />
<TextBlock Text="{Binding SupplierComment}" />
<TextBlock Text="{Binding OEMStatus}" />
</StackPanel>
</DataTemplate>
I found the solution in this post:
WPF DataGrid - Databind to DataTable cell in CellTemplates DataTemplate
You have to use a converter to reach from the table to the actual object.

WPF DataGrid ComboBoxColumn Not Updating Bound Data

I have a dropdown column in my datagrid, the options for the combobox are stored in the tmfCNCComponentStatus_threed table. My main table has a column called [Status] which corresponds to the key column in that table.
The correct status [Description] is displayed in the combobox for each row in my datagrid, but when the user changes the selection, the database isn't updating, even though everything looks as though it is working properly. I have the "UpdateSourceTrigger" set to PropertyChanged as seen in many similar posts but still no dice. Any insight would be greatly appreciated!
<CollectionViewSource x:Key="StatusItems"/>
<DataGridTemplateColumn x:Name="StatusColumn" Header="Status" Width="*" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="cboStatus" Text="{Binding Path=Description}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="StatusCombo" SelectedValuePath="CNCComponentStatusKey" DisplayMemberPath="Description" SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Source={StaticResource StatusItems}}" IsEditable="True" IsSynchronizedWithCurrentItem="True"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
The code behind:
Dim com As String = "SELECT tmfCNCComponent_threed.[CNCComponentKey]
,tmfCNCComponent_threed.[CompanyID]
,tmfCNCComponent_threed.[JobNumber]
,tmfCNCComponent_threed.[LogNumber]
,tmfCNCComponent_threed.[Customer]
,tmfCNCComponent_threed.[DueDate]
,tmfCNCComponent_threed.[JobLeader]
,tmfCNCComponent_threed.[CADProgrammer]
,tmfCNCComponent_threed.[Salesperson]
,tmfCNCComponent_threed.[CNCProgrammer]
,tmfCNCComponent_threed.[ComponentDescription]
,tmfCNCComponent_threed.[ComponentFilePath]
,tmfCNCComponent_threed.[Material]
,tmfCNCComponent_threed.[ComponentSizeX]
,tmfCNCComponent_threed.[ComponentSizeY]
,tmfCNCComponent_threed.[ComponentSizeZ]
,tmfCNCComponent_threed.[QuantityShown]
,tmfCNCComponent_threed.[QuantityMirror]
,tmfCNCComponent_threed.[UpdateTime]
,tmfCNCComponent_threed.[Status]
,tmfCNCComponent_threed.[ProgStarted]
,tmfCNCComponentStatus_threed.[Description]
FROM [test_3DimensionalDB].[dbo].[tmfCNCComponent_threed]
INNER JOIN tmfCNCComponentStatus_threed
ON tmfCNCComponent_threed.Status = tmfCNCComponentStatus_threed.CNCComponentStatusKey
WHERE [ComponentDescription] " & component & " 'trode%' AND [CompanyID]='" & company & "' AND [Status]" & status & "ORDER BY [UpdateTime] DESC"
Dim Adpt As New SqlDataAdapter(com, con)
con.Open()
Dim ds As New DataSet()
Adpt.Fill(ds, "dbo.tmfCNCComponent_threed")
dataGrid1.ItemsSource = ds.Tables("dbo.tmfCNCComponent_threed").DefaultView
con.Close()
con.Open()
Dim statusCVS As CollectionViewSource = FindResource("StatusItems")
Dim com2 As String = "SELECT * FROM tmfCNCComponentStatus_threed"
Dim AdptStatus As New SqlDataAdapter(com2, con)
AdptStatus.Fill(ds, "dbo.tmfCNCComponentStatus_threed")
Dim statusRows = ds.Tables("dbo.tmfCNCComponentStatus_threed").Rows
Dim statuses As New List(Of Object)
For Each row As DataRow In statusRows
statuses.Add(New With {
.Status = CInt(row("CNCComponentStatusKey")),
.Description = CStr(row("Description"))
})
Next
statusCVS.Source = statuses
con.Close()
RowCount()
searchBox.Clear()
Thanks for your time.
The Database:
Contents of the Status table:
The Datagrid:
Here is the first part of this question that was addressed yesterday to get me to this point:
Part 1
Based on information in comments in a different question, you probably need to change SelectedValuePath="Status" to SelectedValuePath="CNCComponentStatusKey". The names of the columns or properties of the items in the combobox are critical to answering this question, and you haven't provided them.
The grid will update the DataRowView column value when the cell leaves edit mode.
Mode=TwoWay on that binding is unnecessary. That's the default mode for bindings on ComboBox.SelectedValue.
You can remove all those decorations from the binding on TextBlock.Text: It can't update the source at all, when you think about it, so there's no need to clutter up your XAML with elaborate instructions about how and when it should do something it can't do anyway.
Complete working example
This is the code with which I tested the above answer. It updates the rows in the main table. It doesn't save the table to a database. That's a separate thing.
I don't know if your columns aren't called what you think they are, or what.
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
LoadData();
}
#region Lists
private static List<String> _status = new List<String>
{
"Ready",
"Not Ready",
"Weary",
"Disordered",
};
private static List<String> _words = new List<String>
{
"Ewwrigowasaus",
"Skubreuph",
"Creecroicr",
"Eunthaudrawtr",
"Ootwoww",
"Meuleetroithr",
"Rewshr",
"Scoysl",
"Scewziexul",
"Kawxayzeec",
};
#endregion Lists
protected void LoadData()
{
DataTable dtMain = new DataTable();
dtMain.Columns.Add("Status", typeof(int));
dtMain.Columns.Add("Programmer", typeof(String));
_words.ForEach(w =>
{
var row = dtMain.NewRow();
row[0] = ((int)w[0] % 2) + 1;
row[1] = w;
dtMain.Rows.Add(row);
});
DataTable dtStatus = new DataTable();
dtStatus.Columns.Add("CNCComponentStatusKey", typeof(int));
dtStatus.Columns.Add("Description", typeof(String));
_status.ForEach(s =>
{
var row = dtStatus.NewRow();
row[0] = dtStatus.Rows.Count + 1;
row[1] = s;
dtStatus.Rows.Add(row);
});
DataGrid.ItemsSource = dtMain.DefaultView;
var cvs = (FindResource("StatusItems") as CollectionViewSource);
cvs.Source = dtStatus.DefaultView;
}
MainWindow.xaml
<Window.Resources>
<CollectionViewSource x:Key="StatusItems" />
</Window.Resources>
<Grid>
<DataGrid x:Name="DataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Programmer}" Header="Programmer" />
<DataGridTemplateColumn Header="Status">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ComboBox
ItemsSource="{Binding Source={StaticResource StatusItems}}"
SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Description"
SelectedValuePath="CNCComponentStatusKey"
x:Name="Combo"
/>
<!-- Selected value in combo -->
<Label Content="{Binding SelectedValue, ElementName=Combo}" />
<!-- Value of Status column in row -->
<Label Content="{Binding Status}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>

Attempting to Databind an ObservableCollection to a ListView the MVVM Way

I'm new to WPF/MVVM and have been attempting to bind an observable collection class to a list view, no code behind, just MVVM.
The XAML:
<Window.DataContext>
<local:acLengthCalcVM/>
</Window.DataContext>
<StackPanel>
<GroupBox Header="Section(s) Selected" Margin="3,10,3,10">
<ListView x:Name="lvSections" Margin="0,6,0,2" Height="150" ItemsSource="{Binding SectionsSelected}">
<ListView.View>
<GridView>
<GridViewColumn Header="#" Width="30"/>
<GridViewColumn Header="Reference Tag #" Width="100"/>
<GridViewColumn Header="Section Type" Width="150"/>
<GridViewColumn Header="Section Length" Width="100"/>
</GridView>
</ListView.View>
</ListView>
</GroupBox>
</StackPanel>
The View Model:
Public Class acLengthCalcVM
Implements INotifyPropertyChanged
Dim _sectionsSelected As ObservableCollection(Of acLengthCalcModel) = New ObservableCollection(Of acLengthCalcModel)
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property SectionsSelected As ObservableCollection(Of acLengthCalcModel)
Get
Return _sectionsSelected
End Get
Set(value As ObservableCollection(Of acLengthCalcModel))
_sectionsSelected = value
NotifyPropertyChanged("SectionsSelected")
End Set
End Property
Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
The Model:
Public Class acLengthCalcModel
Inherits ObservableCollection(Of acLengthCalcData)
Public Sub New()
Add(New acLengthCalcData(1, "n/a", "VTU Frame", 8))
Add(New acLengthCalcData(2, "1.1.01", "Air Conveyor Straight", 118))
End Sub
End Class
Public Class acLengthCalcData
' Constructor for this Class
Public Sub New(serialNum As Integer, refTagNum As String, sectionType As String, sectionLength As Double)
_refTagNum = refTagNum
_sectionLength = sectionLength
_sectionType = sectionType
_serialNum = serialNum
End Sub
' Reference Tag #
Private _refTagNum As String
Public Property RefTagNum As String
Get
Return _refTagNum
End Get
Set(value As String)
_refTagNum = value
End Set
End Property
' Section Length
Private _sectionLength As Double
Public Property SectionLength As Double
Get
Return _sectionLength
End Get
Set(value As Double)
_sectionLength = value
End Set
End Property
' Section Type
Private _sectionType As String
Public Property SectionType As String
Get
Return _sectionType
End Get
Set(value As String)
_sectionType = value
End Set
End Property
' Serial #
Private _serialNum As Integer
Public Property SerialNum As Integer
Get
Return _serialNum
End Get
Set(value As Integer)
_serialNum = value
End Set
End Property
End Class
When I run the code, the list view is created with only its headers, none of the data I initialized in the model constructor shows up.
Any pointers? Thanks.
You should create and return an instance of a acLengthCalcModel in your view model class:
Public Class acLengthCalcVM
Implements INotifyPropertyChanged
Dim _sectionsSelected = New acLengthCalcModel
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property SectionsSelected As acLengthCalcModel
Get
Return _sectionsSelected
End Get
Set(value As acLengthCalcModel)
_sectionsSelected = value
NotifyPropertyChanged("SectionsSelected")
End Set
End Property
Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
You also need to bind each of the columns to a property of the acLengthCalcData class using the DisplayMemberBinding property:
<ListView x:Name="lvSections" Margin="0,6,0,2" Height="150" ItemsSource="{Binding SectionsSelected}">
<ListView.View>
<GridView>
<GridViewColumn Header="#" DisplayMemberBinding="{Binding SerialNum}" Width="30"/>
<GridViewColumn Header="Reference Tag #" DisplayMemberBinding="{Binding RefTagNum}" Width="100"/>
<GridViewColumn Header="Section Type" DisplayMemberBinding="{Binding SectionType}" Width="150"/>
<GridViewColumn Header="Section Length" DisplayMemberBinding="{Binding SectionLength}" Width="100"/>
</GridView>
</ListView.View>
</ListView>

How to add item in Listview on WPF

I can add column in ListView, but it is so difficult to add an item in ListView. Here is my code:
Dim myGridView As New GridView
myGridView.AllowsColumnReorder = True
myGridView.ColumnHeaderToolTip = "Employee Information"
Dim gvc1 As New GridViewColumn
gvc1.DisplayMemberBinding = New Binding("FirstName")
gvc1.Header = "FirstName"
gvc1.Width = 100
myGridView.Columns.Add(gvc1)
Dim gvc2 As New GridViewColumn
gvc2.DisplayMemberBinding = New Binding("LastName")
gvc2.Header = "Last Name"
gvc2.Width = 100
myGridView.Columns.Add(gvc2)
Dim gvc3 As New GridViewColumn()
gvc3.DisplayMemberBinding = New Binding("EmployeeNumber")
gvc3.Header = "Employee No."
gvc3.Width = 100
listview.View = myGridView
I just created ListViewItem.add and then used the subitem to add more items in a row. But now it's different. How can I add item in listview in WPF WITHOUT creating a new class, because the number of the columns of data is dynamic.
EDIT:
I had been surfing the internet for hours but found nothing of great help.
Everywhere, it is done using class with predefined numbers of column, while I want to add column item based on database which should have different Ipnumber of columns.
You should initialize the object collection in your code, for example Employees.
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Public Class EmployeeInformation
Public Property FirstName As String
Public Property LastName As String
Public Property EmployeeNumber As Integer
End Class
Class MainWindow
Public Property Employees As ObservableCollection(Of EmployeeInformation)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
InitEmploeesCollection(10)
DataContext = Me
End Sub
Private Sub InitEmploeesCollection(count As Integer)
Employees = New ObservableCollection(Of EmployeeInformation)()
For index = 1 To count
Employees.Add(New EmployeeInformation() With {
.FirstName = "FirstName" & index,
.LastName = "LastName" & index,
.EmployeeNumber = index})
Next
End Sub
End Class
And then you can simply bind it to the ListView:
<ListView ItemsSource="{Binding Path=Employees}">
<ListView.View>
<GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Employee Information">
<GridViewColumn Header="First Name" Width="100" DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Header="Last Name" Width="100" DisplayMemberBinding="{Binding Path=LastName}"/>
<GridViewColumn Header="Employee No." Width="100" DisplayMemberBinding="{Binding Path=EmployeeNumber}"/>
</GridView>
</ListView.View>
</ListView>
To initialize columns dynamically, you can add the code from your question. It doesn't make difference if you are taking data from database. Just fill in Employees collection and bind it to the ListView.
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Class MainWindow
Public Property Employees As ObservableCollection(Of EmployeeInformation)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
InitEmploeesCollection(10)
SetGridViewDynamically()
DataContext = Me
End Sub
Private Sub InitEmploeesCollection(count As Integer)
Employees = New ObservableCollection(Of EmployeeInformation)()
For index = 1 To count
Employees.Add(New EmployeeInformation() With {
.FirstName = "FirstName" & index,
.LastName = "LastName" & index,
.EmployeeNumber = index})
Next
End Sub
Private Sub SetGridViewDynamically()
Dim myGridView As New GridView
myGridView.AllowsColumnReorder = True
myGridView.ColumnHeaderToolTip = "Employee Information"
Dim gvc1 As New GridViewColumn
gvc1.DisplayMemberBinding = New Binding("FirstName")
gvc1.Header = "FirstName"
gvc1.Width = 100
myGridView.Columns.Add(gvc1)
Dim gvc2 As New GridViewColumn
gvc2.DisplayMemberBinding = New Binding("LastName")
gvc2.Header = "Last Name"
gvc2.Width = 100
myGridView.Columns.Add(gvc2)
Dim gvc3 As New GridViewColumn()
gvc3.DisplayMemberBinding = New Binding("EmployeeNumber")
gvc3.Header = "Employee No."
gvc3.Width = 100
myGridView.Columns.Add(gvc3)
ListView1.View = myGridView
End Sub
End Class
Public Class EmployeeInformation
Public Property FirstName As String
Public Property LastName As String
Public Property EmployeeNumber As Integer
End Class
This way XAML will look like this.
<ListView Name="ListView1" ItemsSource="{Binding Path=Employees}"/>
Dim row As String() = New String() {"John", "Doe", "1"}
myGridView.Rows.Add(row)
Is this what you're looking for?
XAML part of your program must be like be this:
<ListView x:Name="lvPencere" HorizontalAlignment="Left" Height="156" Grid.Row="1" VerticalAlignment="Top" Width="309">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="PencereSN" Width="0" DisplayMemberBinding="{Binding PencereSN}"/>
<GridViewColumn Header="Pencere Adı" Width="300" DisplayMemberBinding="{Binding PencereAD}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
And your VB part must be like this:
Listview.Items.Add(New With {Key .PencereSN = "some string", Key .PencereAD = "some string"})

Not able to make cascading comboboxes work

Here is my code to create cascading comboboxes. I am trying to populate Family Combobox(ComboBox2) based on the value selected for Segment Name(combox1).I am able to populate the first Combo with a static resource but my second one shows blank.
I am calling a method called "FillComboBoxFamilyData(SegmentCode)" on the selection change event of first combobox.
XAML CODE:
<Grid Height="142" HorizontalAlignment="Left" Margin="49,113,0,0" Name="grid3" VerticalAlignment="Top" Width="904">
<ComboBox Height="23" HorizontalAlignment="Left" Margin="35,26,0,0" Name="comboBox1" VerticalAlignment="Top" Width="205" ItemsSource="{Binding Source={StaticResource tblSegmentViewSource}}" DisplayMemberPath="Segment Name" SelectedValuePath="Segment Code" SelectionChanged="comboBox1_SelectionChanged"/>
<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,26,395,0" Name="comboBox2" VerticalAlignment="Top" Width="205" />
<Window.Resources>
<CollectionViewSource x:Key="tblSegmentViewSource" Source="{Binding Path=TblSegment, Source={StaticResource brickDataset}}" />
<CollectionViewSource x:Key="tblFamilyViewSource" Source="{Binding Path=TblFamily, Source={StaticResource brickDataset}}" />
*BrickDataSet is the main dataset I am pulling the tables from.*
C#:
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
SegmentCode = Convert.ToInt32(comboBox1.SelectedValue.ToString());
FillComboBoxFamilyData(SegmentCode);
}
public void FillComboBoxFamilyData(int Segment_Code)
{
string connString =
"Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\dchaman\\My Documents\\PDRT.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True ";
SqlConnection con = new SqlConnection(connString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText =
"SELECT TblFamily.[Family Name],TblFamily.[Family Code] FROM TblFamily WHERE (TblFamily.[Segment Code] = #SegmentCode)";
cmd.Parameters.AddWithValue("#SegmentCode", Segment_Code);
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
dAdapter.Fill(objDs);
con.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
MessageBox.Show("Here I am");
comboBox2.DataContext = objDs.Tables;
comboBox2.Items.Insert(0, "--Select Family Name--");
comboBox2.DisplayMemberPath = "Family Name";
comboBox2.SelectedValue = "Family Code";
}
}
I am banging my head with the table right now.Please Save me!
You aren't setting the comboBox2's ItemsSource. The DataContext will simply effect all binding statements on it. If you set the ItemsSource to the correct table instead of the DataContext, it should get you on the right path:
if (objDs.Tables[0].Rows.Count > 0)
{
MessageBox.Show("Here I am");
comboBox2.ItemsSource = ((IListSource)objDs.Tables[0]).GetList(); // set the ItemsSource instead of the DataContext
comboBox2.DisplayMemberPath = "Family Name";
comboBox2.SelectedValue = "Family Code";
}
Note that when you set the ItemsSource, the line that inserts text into the Items property will fail, as you can not use both the ItemsSource and the Items together.

Resources