Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

datagridcombobox binding

$
0
0

I am using visual studio 2010 for a wpf project.  I have a datagrid with two columns, Lab and LabType.  I want LabType to be a DataGridComboBox column with values in the dropdown list dependent upon the value of Lab in the current row (there may or may not be common values in the dropdown for each row).  I have something that kind of works, with a code snippet below.  I am setting grid DataContext and the combobox ItemsSource in the code behind to and arraylist, which gets updated when the row changes. When first loaded the "LabType" column is empty (all rows have a value in the underlying datatable - the combobox is meant for changing it to another valid value).  If I select a cell in the LabType column its combobox dropdown is populated correctly and once a value is selected it shows up in the grid.  The value in the LabType will also show up in other rows if the underlying data also matches that selection.

I want the grid to always show the LabType from the underlying datatable and for the combobox to present the user a list of other valid values for LabType. Can anyone see where I am going wrong?  thanks

in xaml<DataGrid Padding="3" AutoGenerateColumns="False" Name="dgLab" ItemsSource="{Binding}" SelectionChanged="dgLab_SelectionChanged"><DataGrid.Columns><DataGridTextColumn Header="Lab" Binding="{Binding LAB}"></DataGridTextColumn><DataGridComboBoxColumn x:Name="dgLabType" Header="LabType" Width="Auto" TextBinding="{Binding LabType}"></DataGridComboBoxColumn></DataGrid.Columns></DataGrid>

in code behind
public ArrayList aLabType = new ArrayList() //public variable defined for class

private void populateALabType(string sLab)
{	
	aLabType.Clear();
	string sValue = string.Empty;
	DataSet ds = SAFDataSource.GetQCListLabMethodMatrix(sLabCode);
	foreach (DataRow dr in ds.Tables[0].Rows)
	{
		sValue = dr[0].ToString();
		aLabType.Add(sValue);
	}
	dgQC.ItemsSource = aLabType;	
}

private void dgLab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{	
	DataRowView drPA = dgLab.CurrentItem as DataRowView;
	if (drPA != null)
	{
		string sLab = drPA["LAB"].ToString();			
		populateALabType(sLab);
	}
}

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>