How do I check to see if a child form is already displayed so I don’t have two instances showing?

Here are two ways you can do this. i) Within the parent MDI form, use code such as this: // MyChildForm is the one I’m looking for MyChildForm childForm = null; foreach(Form f in this.MdiChildren) { if(f is MyChildForm) { // found it childForm = (MyChildForm) f; break; } } if( childForm != null) { childForm.Show(); childForm.Focus(); } else { childForm = new MyChildForm(); childForm.MdiParent = this; childForm.Show(); childForm.Focus(); } ii) Here is a second solution suggested by John Conwell that implements a singleton pattern on the child form. In the MDI Child form put this code in (where frmChildForm is the MDI child form you want to control) //Used for singleton pattern static frmChildForm childForm; public static ChildForm GetInstance { if (childForm == null) childForm = new frmChildForm; return childForm; } In the Parent MDI form use the following code to call the child MDI form frmChildForm childForm = frmChildForm.GetInstance(); childForm.MdiParent = this; childForm.Show(); childForm.BringToFront(); The first time this code is called, the static GetInstance method will create and return an instance of the child form. Every other time this code is called, the GetInstance method will return the existing instance of the child from, stored in the static field childForm. If the child form instance is ever destroyed, the next time you call GetInstance, a new instance will be created and then used for its lifetime. Also, if you need constructors or even overloaded constructors for your MDI Child Form, just add the needed parameters to the GetInstance function and pass them along to the class constructor.

How do I iterate through all the rows and columns in my datagrid

To iterate through all rows and columns in a WinForms DataGrid, you can access the underlying data using the grid’s view records and columns. The following code example demonstrates how to achieve this by looping through each record and column, retrieving its cell values and printing them to the console. [C#] [VB.NET]

How can I drag a window if it doesn’t have a title bar or border

You can drag such a window by using Win32 APIs to switch the mouse hit to WM_NCLBUTTONDOWN. The code below will allow you to drag by mousing down anywhere in the form’s clientarea as long as you don’t hit a child control on the form. using System.Runtime.InteropServices; ………… public const int WM_NCLBUTTONDOWN = 0xA1; public const int HTCAPTION = 0x2; [DllImportAttribute (‘user32.dll’)] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImportAttribute (‘user32.dll’)] public static extern bool ReleaseCapture(); private void Form2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } Here is a solution posed by Jacob Grass (Abiliti Solutions) that does not rely on InteropServices. Private blnMoving As Boolean = False Private MouseDownX As Integer Private MouseDownY As Integer Private Sub Form1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles Form1.MouseDown If e.Button = MouseButtons.Left Then blnMoving = True MouseDownX = e.X MouseDownY = e.Y End If End Sub Private Sub Form1_MouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles Form1.MouseUp If e.Button = MouseButtons.Left Then blnMoving = False End If End Sub Private Sub Form1_MouseMove(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles Form1.MouseMove If blnMoving Then Dim temp As Point = New Point() temp.X = Form1.Location.X + (e.X – MouseDownX) temp.Y = Form1.Location.Y + (e.Y – MouseDownY) Form1.Location = temp End If End Sub

How can I prevent my user from sizing columns in my datagrid

You can do this by subclassing your grid and overriding OnMouseMove, and not calling the baseclass if the point is on the columnsizing border. [C#] public class MyDataGrid : DataGrid { protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) { DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y)); if(hti.Type == DataGrid.HitTestType.ColumnResize) { return; //no baseclass call } base.OnMouseMove(e); } } [VB.NET] Public Class MyDataGrid Inherits DataGrid Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs) Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y)) If hti.Type = DataGrid.HitTestType.ColumnResize Then Return ’no baseclass call End If MyBase.OnMouseMove(e) End Sub End Class The above code prevents the sizing cursor from appearing, but as Stephen Muecke pointed out to us, if the user just clicks on the border, he can still size the column. Stephen’s solution to this problem is to add similar code in an override of OnMouseDown. [C#] protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) { DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y)); if(hti.Type == DataGrid.HitTestType.ColumnResize) { return; //no baseclass call } base.OnMouseDown(e); } [VB.NET] Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y)) If hti.Type = DataGrid.HitTestType.ColumnResize Then Return ’no baseclass call End If MyBase.OnMouseDown(e) End Sub

How do I set default values for new rows in my datagrid

You use the DataColumn.DefaultValue property to provide default values for new rows. You access this property through the DataTable associated with the DataGrid, [C#] this.dataGrid1.DataSource = this._dataSet.Tables[‘orders’]; …. …. this._dataSet.Tables[‘Orders’].Columns[1].DefaultValue = ‘CustID’; // default value for column 1 this._dataSet.Tables[‘Orders’].Columns[‘OrderDate’].DefaultValue = DateTime.Now; // default value for OrderDate column [VB.NET] Me.dataGrid1.DataSource = Me._dataSet.Tables(‘Orders’) …. …. Me._dataSet.Tables(‘Orders’).Columns(1).DefaultValue = ‘CustID’ ’ default value for column 1 Me._dataSet.Tables(‘Orders’).Columns(‘OrderDate’).DefaultValue = DateTime.Now ’ default value for OrderDate column