I have code snippets that I use often. How can I manage them so I can easily used them
In Visual Studio, you can store commonly used code snippets on a tab (other than the Clipboard ring) in your Toolbox. You use drag and drop techniques to move the snippets to the Toolbox. To later use a snippet in your code, you drag it from the Toolbox and drop it into your code.
How can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key
You can handle this by subclassing your grid and overriding either PreProcessMessage or ProcessDialogKey. The code below assumes your datasource is a dataview. If it is not, you could just remove that check [C#] public override bool PreProcessMessage( ref Message msg ) { Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode; if(msg.Msg == WM_KEYDOWN && keyCode == Keys.Delete && ((DataView) this.DataSource).AllowDelete) { if(MessageBox.Show(‘Delete this row?’, ”, MessageBoxButtons.YesNo) == DialogResult.No) return true; } return base.PreProcessMessage(ref msg); } [VB.NET] (courtesy of Erik Johansen) Public Class DataGrid_Custom Inherits DataGrid Private Const WM_KEYDOWN = &H100 Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean Dim keyCode As Keys = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys) If msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then If MessageBox.Show(‘Delete This Row?’, ‘Confirm Delete’, MessageBoxButtons.YesNo) = DialogResult.No Then Return True End If End If Return MyBase.PreProcessMessage(msg) End Function End Class
How can I get the number of rows in my DataGrid
One way you can do this is through the BindingManager.Count property. [C#] int numRows = dataGridDetails.BindingContext[dataGridDetails.DataSource, dataGridDetails.DataMember].Count; [VB.NET] Dim numRows as Integer = i dataGridDetails.BindingContext(dataGridDetails.DataSource, dataGridDetails.DataMember).Count;
How do I dynamically load a control from a DLL
You use System Reflection to dynamically load a control. If the DLL is named ‘SpecControls.DLL’ and the class you want is ‘SpecControls.ColorControl’, then use this code. [C#] // load the assembly System.Reflection.Assembly assembly = Assembly.LoadFrom(‘SpecControls.DLL’); // get the type Type t = assembly.GetType(‘SpecControls.ColorControl’); // create an instance and add it. // Control c = (Control)Activator.CreateInstance(t); parent.Controls.Add(c); [VB.NET] ’ load the assembly Dim assembly1 As System.Reflection.Assembly = Assembly.LoadFrom(‘SpecControls.DLL’) ’ get the type Dim t As Type = assembly1.GetType(‘SpecControls.ColorControl’) ’ create an instance and add it. ’ Dim c As Control = CType(Activator.CreateInstance(t), Control) parent.Controls.Add(c)
How do I get the row or column that has been clicked on?
You can use the DataGrid’s HitTest method, passing it a point in the grid’s client coordinate system, and returning a HitTestInfo object that holds all the row and column information that you want. [C#] // X & Y are in the grid’ coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method System.Drawing.Point pt = new Point(X, Y); DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt); if(hti.Type == DataGrid.HitTestType.Cell) { MessageBox.Show(dataGrid1[hti.Row, hti.Column].ToString()); } else if(hti.Type == DataGrid.HitTestType.ColumnHeader) { MessageBox.Show(((DataView) DataGrid1.DataSource).Table.Columns[hti.Column].ToString()); } [VB.NET] ’ X & Y are in the grid’ coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method Dim pt = New Point(X, Y) Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt) If hti.Type = DataGrid.HitTestType.Cell Then MessageBox.Show(dataGrid1(hti.Row, hti.Column).ToString()) Else If hti.Type = DataGrid.HitTestType.ColumnHeader Then ’assumes datasource is a dataview MessageBox.Show(CType(DataGrid1.DataSource, DataView).Table.Columns(hti.Column).ToString()) End If End If