When I click on a row header, the row is selected and no cell is active. How can I do this programmatically
This code adds a method to a derived grid that simulates a mouseclick on the rowheader of the row passed into the method. public class MyDataGrid : DataGrid { public const int WM_LBUTTONDOWN = 513; // 0x0201 public const int WM_LBUTTONUP = 514; // 0x0202 [System.Runtime.InteropServices.DllImport(‘user32.dll’)] static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam); public void ClickRowHeader(int row) { //get a point to click Rectangle rect = this.GetCellBounds(row, 0); Int32 lparam = MakeLong(rect.Left – 4, rect.Top + 4); //click it SendMessage( this.Handle, WM_LBUTTONDOWN, 0, lparam); SendMessage( this.Handle, WM_LBUTTONUP, 0, lparam); } static int MakeLong(int LoWord, int HiWord) { return (HiWord << 16) | (LoWord & 0xffff); } } ….. ….. //usage – myDataGrid is of type MyDataGrid. private void button2_Click(object sender, System.EventArgs e) { myDataGrid.ClickRowHeader(2); }
How can I translate a point to a cell in the datagrid
If the point, say pt, is in screen coordinates, you can use code such as Point p1 = dataGrid1.PointToClient(pt); MessageBox.Show(dataGrid1.HitTest(p1).ToString()); If you are using context menus for catching right-clicks in the grid, you have to do a little work to remember where the original right-click point was as the point passed in through the menu event arguments may not reflect this original click. One solution is to remember the click in the grid MouseDown event, and then use the code above to retrieve the grid cell from within the menu handler. Point rightMouseDownPoint; private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Right) rightMouseDownPoint = Cursor.Position; } private void menuItem4_Click(object sender, System.EventArgs e) { Point pt = dataGrid1.PointToClient(rightMouseDownPoint); MessageBox.Show(dataGrid1.HitTest(pt).ToString()); }
How can I work around my menu shortcuts from showing up incorrectly when I use Ctrl+Number?
When you assign Ctrl1, Ctrl2 etc as shortcuts for menuitems they show up as Ctrl+D1, Ctrl+D2. This can be worked around by creating and adding the menuitem through code as demonstrated below: [C#] //Create the menuitem MenuItem mymenuItem = new MenuItem(); //ShortCut mymenuItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl1; //Add Event Handler for the menuitem mymenuItem.Click +=new EventHandler(this.mymenuItem_Click); //ShortCut Text to be displayed mymenuItem.Text = ‘My MenuItem’ +’\t’+ ‘Ctrl+1’; //hide shortcut mymenuItem.ShowShortcut = false; //Add it to the bottom of the first menu this.mainMenu1.MenuItems[0].MenuItems.Add(mymenuItem); [VB.NET] ’Create the menuitem Dim mymenuItem As MenuItem = New MenuItem() ’ShortCut mymenuItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl1 ’Add Event Handler for the menuitem mymenuItem.Click +=New EventHandler(Me.mymenuItem_Click) ’ShortCut Text to be displayed mymenuItem.Text = ‘My MenuItem’ +’\t’+ ‘Ctrl+1’ ’hide shortcut mymenuItem.ShowShortcut = False ’Add it to the bottom of the first menu Me.mainMenu1.MenuItems(0).MenuItems.Add(mymenuItem)
What is the function of TypeDescriptor?
Here is an MSDN article that discuss about TypeDescriptor and its capabilities, Type Descriptor Overview in msdn
What is the (DynamicProperties) item listed on a control’s property page in VS.NET
Clicking Advanced… under this DynamicProperties option in the control properties displays certain control properties that can be set through an XML app.config file that is added to your project. This file stores the dynamic property values and is automatically read during the initialization process on the form at design time. You can manually edit this file to change the properties and next when you run the application, controls will pick up the new values. This file is used strictly for design time initializations.