Reputation: 29
How can I get data from DataGrid when ComboBox' text is equal with the record of the DataGrid:
combobox1.ItemsSource = database.Mahs.ToList();
combobox1.DisplayMemberPath = "MahName";
and
private void datagrid_customer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var data = datagrid_customer.SelectedItem;
string id = (datagrid_customer.SelectedCells[0].Column.GetCellContent(data) as TextBlock).Text;
txt_f1.Text = id;
}
It shows me the id, but when i selected item but i want show me id when combobox.Text = name of the row in the DataGrid, then show the id of that row.
Upvotes: 0
Views: 148
Reputation: 1279
I would suggest changing your approach slightly, and retrieving the entire object from both controls for the comparison. This way you have full access to the properties of each selected object.
If the objects you're retrieving from your database override .Equals
and .GetHashCode
you could do away with some of the if statements below. But to get you started here's a quick example of your change listener
private void datagrid_customer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Cast the objects from your DataGrid and your ComboBox.
YourDataObject dataItem = (YourDataObject)dataGrid1.SelectedItem;
YourDataObject comboItem = (YourDataObject)combo.SelectedItem;
// Compare your objects and decide if they're the same. Ideally you would
// have Equals and HashCode overridden so you could improve this
if (dataItem == null || comboItem == null)
text.Text = "Not Matching";
else
{
if (dataItem.MahName == comboItem.MahName)
// You've got full access to the object and all it's properties
text.Text = dataItem.Id.ToString();
else
text.Text = "Not Matching";
}
}
Upvotes: 1