---
title: "Data Binding Woes - A Simple Solution"
url: "https://osherove.com/blog/2003/6/15/data-binding-woes-a-simple-solution.md"
canonical: "https://osherove.com/blog/2003/6/15/data-binding-woes-a-simple-solution.html"
published: "2003-06-15T18:59:00"
updated: "2003-06-15T18:59:00"
tags: [".Net Original", "Cool Articles"]
author: "Roy Osherove"
description: "Wooho! Found the solution to the Data Binding problem I was having . I got it from this free book chapter: How to use Data Views from this book . I'll explain the problem and then the solution. Problem : How do you get…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Data Binding Woes - A Simple Solution

By Roy Osherove June 15, 2003 In [.Net Original](https://osherove.com/blog/category/net-original), [Cool Articles](https://osherove.com/blog/category/cool-articles)

**Historical article** · Originally published June 15, 2003; technical details may be outdated.

Wooho! Found the solution to the [Data Binding problem I was having](http://weblogs.asp.net/rosherove/posts/8699.aspx). I got it from this free book chapter: [How to use Data Views](http://www.ftponline.com/books/chapters/1890774197.pdf)  from [this book](https://www.amazon.com/exec/obidos/ASIN/1890774197/qid=1055453215/sr=8-1/ref=sr_8_1/iserializable-20).

I'll explain the problem and then the solution.

**Problem**: How do you get the desired position in a BindingContext, when all you have to go on is a DataRow object?

This problem would occur whenever you wanted a control other that a DataGrid to serve as a 'master' in a master-details data bound form. Imagine that the control that you have is a treeview. You filled up the treeNodes and set the node.Tag property with a DataRow object . You want the user to be able to click on a tree node and let a DataGrid on the form show the child rows of the selected DataRow.

Now, If we have a ListBox or ComboBox control, this could be easy. You just set the BindingManager's Position property to the current index of the control. But what about a treeview? Where is the $#%! index there?

**The solution** is pretty darn simple: using a _DataView_ object!

A DataView object has the ability to find a row based on a key and a value, meaning that if the DataRow is sorted according to the primary key of the table, you can use it too look up a row based on a given primary key. It contains a **Find** method which, how comfortable, return the index to the row that was found. so, all you have to do is create a new DataView object, make it sort on your PK, and use it to find and index. Then you set the Binding manager's postion according to the returned index. Yu will need to bind your controls to that particular DataView, so you'll get the correct position(no biggie). One word of caution - a DataTable object has by default a DefaultDataView property which you can use. It is recommended to use a new DataView object, which gives you more flexibilty, and hey, you can create 2 or even 10 different view of your data. You can sort them, you can filter them, hell, you can just show the rows that were last changed,added,deleted - you name it. It's all there using the DataView.

I just "got" it, and I like it!

Here's the code to handle the user selecting a tree node:

---

//Create a custom view of the data

private DataView view = new DataView(m\_ds.Tables\["Stuff"\]);

//make it sort based on the PK

view.Sort\="ID";

//Handling a listbox event

private void listBox1\_SelectedIndexChanged(object sender, System.EventArgs e)

{

      //get the binding manager to set the position

      BindingManagerBase bind = BindingContext\[m\_ds,"Stuff"\];

      //Using the 'Find' of the DataView

      //returns the needed row index!

      //Just make sure you bind the ValueMember

      //property of the listbox

      bind.Position\= view.Find(listBox1.SelectedValue);

}

//Handling a TreeView event

private void treeView1\_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)

{

      //get the binding manager to set the position

      BindingManagerBase bind = BindingContext\[m\_ds,"Stuff"\];

      DataRow row = (DataRow)treeView1.SelectedItem.Tag;

      int wantedID = int.Parse(row\["ID"\].ToString());

      bind.Position\= view.Find(wantedID);

}

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Jun 19, 2003

    ### [Winforms DataBinding Bliss](https://osherove.com/blog/2003/6/19/winforms-databinding-bliss.html)

- Jun 16, 2003

    ### [Funny Behaviour](https://osherove.com/blog/2003/6/16/funny-behaviour.html)

- Jun 14, 2003

    ### [Winforms Data Binding Woes](https://osherove.com/blog/2003/6/14/winforms-data-binding-woes.html)

- Jun 13, 2003

    ### [About Reflector, Encoding, And an Article Error](https://osherove.com/blog/2003/6/13/about-reflector-encoding-and-an-article-error.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
