How do I bind a TextBox to a nested relation
Say you have a Parent table related to a Child table related to a GrandChildTable, and you want to bind a TextBox to a column in the GrandChild table. To do this you have to have nested relations defined, and you use this nesting to create the DataMember parameter for the the DataBinding that you add to the TextBox. Below are some code snippets. They show the DataMember as ”ParentToChild.ChildToGrandChild.Name” which creates a path from the parent table down to the field in the nested relation by stringing the relation names together separated with a period. You can also download both C# and VB projects. Dim dSet As New DataSet() ’get the tables Dim parentTable As DataTable = GetParentTable() Dim childTable As DataTable = GetChildTable() Dim grandChildTable As DataTable = GetGrandChildTable() dSet.Tables.AddRange(New DataTable() {parentTable, childTable, grandChildTable}) ’setup the relations Dim parentColumn As DataColumn = parentTable.Columns(”parentID”) Dim childColumn As DataColumn = childTable.Columns(”ParentID”) dSet.Relations.Add(”ParentToChild”, parentColumn, childColumn) parentColumn = childTable.Columns(”childID”) childColumn = grandChildTable.Columns(”ChildID”) dSet.Relations.Add(”ChildToGrandChild”, parentColumn, childColumn) Me.TextBox1.DataBindings.Add(”Text”, parentTable, ”ParentToChild.ChildToGrandChild.Name”)
How can I display master-details-details in three separate grids
Here is a sample (both VB and C#) that illustrates how to have a parent table which has a related child table, which also has a related grandchild table. Below are some code snippets. The trick is to always make the main parent table be the DataSource for all the grid, and then set the DataMember to be the relation name where teh relation starts at the parent table. This means the DisplayMember for the Child table is ”ParentToChild”, the name of that relation. And, the DisplayMember for the grandchild grid is ”ParentToChild.ChildToGrandChild” which defines the relation starting at the parent grid through the child grid. Dim dSet As New DataSet() ’get the tables Dim parentTable As DataTable = GetParentTable() Dim childTable As DataTable = GetChildTable() Dim grandChildTable As DataTable = GetGrandChildTable() dSet.Tables.AddRange(New DataTable() {parentTable, childTable, grandChildTable}) ’setup the relations Dim parentColumn As DataColumn = parentTable.Columns(”parentID”) Dim childColumn As DataColumn = childTable.Columns(”ParentID”) dSet.Relations.Add(”ParentToChild”, parentColumn, childColumn) parentColumn = childTable.Columns(”childID”) childColumn = grandChildTable.Columns(”ChildID”) dSet.Relations.Add(”ChildToGrandChild”, parentColumn, childColumn) ’set the grids Me.dataGrid1.DataSource = parentTable Me.dataGrid2.DataSource = parentTable Me.dataGrid2.DataMember = ”ParentToChild” Me.dataGrid3.DataSource = parentTable Me.dataGrid3.DataMember = ”ParentToChild.ChildToGrandChild” Me.dataGrid1.AllowNavigation = False Me.dataGrid2.AllowNavigation = False Me.dataGrid3.AllowNavigation = False
How can I tell which column (subitem) has been clicked on in my listview
Here is a solution offered by Bharat Patel of Microsoft in the microsoft.public.dotnet.languages.csharp newgroup. The attached solution contains both C# and VB.NET projects. The solution use Interop with the LVM_GETSUBITEMRECT window’s message to loop through each subitem’s bounds rectangle until it finds the one that contains the specified mouse click point.
How can I convert a long path to a short path?
There is no direct support in the framework to do this. You have to use the GetShortPathName function using PInvoke. This is how the signature for this function looks like: [DllImport(”kernel32.dll”, SetLastError=true, CharSet=CharSet.Auto)] public static extern int GetShortPathName(string longPath, [MarshalAs(UnmanagedType.LPTStr)]StringBuilder ShortPath, [MarshalAs(UnmanagedType.U4)]int bufferSize); Download C# Sample GetShortPathName.zip Download VB.NET sample GetShortPathName_VB.zip
How can I use a mouse to select cell ranges in my datagrid
The Windows Forms DataGrid does not support the selection of a range of cells other than a group of rows. To select a row, you can click on its row header. But if you want to select a rectangular group of cells, you cannot. To add this support, you can catch the mousedown on a cell, and in mousemove, track whether the mouse is dragged with the button down to other cells. If so, draw a selection rectangle over this group of cells. The attached samples (C#, VB) show how this might be done. The drawing of the selection rectangle is done in an OnPaint override. The samples also try to handle autoscrolling as you hit a grid border so the selection process can continue for cells that are not currently visible. To make the selection rectangle look nicer, the code also hides the current cell when it draws a selection rectangle. The samples have a derived DataGrid that has a public member, SelectedRange, that holds the TopLeft and BottomRight coordinates of the selected range. The derived grid also has a public event, SelectionChanging, that fires prior to the selection changing as you drag your mouse. The event passes both the old selection and the new selection, and allows you to cancel the action if you choose.