How do I set the width of a column in my DataGrid
To set a column width, your datagrid must be using a non-null DataGridTableStyle. Once this is in place, you can set the column width by first getting the tablestyle and then using that object to obtain a column style with which you can set the width. Here are some code snippets showing how you might do this. //…. make sure your DataGrid is using a tablestyle dataGrid1.DataSource = _dataSet.Tables[‘customers’]; DataGridTableStyle dgts = new DataGridTableStyle(); dgts.MappingName = ‘customers’; dataGrid1.TableStyles.Add(dgts); //…… //method to set a column with by colnumber public void SetColWidth(DataGridTableStyle tableStyle, int colNum, int width) { try { tableStyle.GridColumnStyles[colNum].Width = width; tableStyle.DataGrid.Refresh(); } catch{} //empty catch .. do nothing } //…. // here is how you might call this method private void button1_Click(object sender, System.EventArgs e) { DataGridTableStyle tableStyle = dataGrid1.TableStyles[‘customers’]; SetColWidth(tableStyle, 1, 200); }
How can I prevent the Enter key from moving to the next cell when the user is actively editing the cell and presses Enter
Override the method ProcessKeyPreview in your DataGrid. protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m) { Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode; if((m.Msg == WM_KEYDOWN || m.Msg == WM_KEYUP) && keyCode == Keys.Enter ) return false; return true; }
How do I prevent my Control’s properties from getting combined with properties belonging to other objects in a Properties window?
Use the Mergable(false) attribute. This will prevent your Property from being edited as part of a group in the designer.
How do I prevent the default values of my Localized properties form being set?
It is normal to have Properties in your Control/Component whose default values are inherited from some other Control/Component. In such cases you will normally prevent the designer from storing the property’s value in code (using either DefaultValue attribute or the ShouldSerializeXXX pattern). However, if that property is Localizable and Localization is turned on, then the property’s value will be forced to be stored in the resource. This will break your property-inheritance logic. For example: [ Localizable(true) … ] public Font MyControlButtonFont { get { if(this.buttonFont == null) return this.Font; else return this.buttonFont; } set { this.buttonFont = value; } } private bool ShouldSerializeMyControlButtonFont() { if(this.MyControlButtonFont == this.Font) return false; else return true; } In the above case the MyControlButtonFont inherits its value from the Font property, if its value is not set. And you use null to determine whether the value is set or not. But when Localization is ON, the property gets SET and you lose the inheritance logic. You can avoid this by specifying an AmbientValue attribute for your property, as follows: [ Localizable(true), AmbientValue(null) … ] public Font MyControlButtonFont This will use the AmbientValue as the value to persist when there is default-value in your property. This will prevent your property from getting SET unnecessarily.
How do I get the row and column of the current cell in my datagrid
You can use the CurrentCell property of the DataGrid. private void button1_Click(object sender, System.EventArgs e) { intcolNum = dataGrid1.CurrentCell.ColumnNumber; int rowNum = dataGrid1.CurrentCell.RowNumber; object cellValue = dataGrid1[rowNum, colNum]; string s = string.Format(‘row={0} col={1} value={2}’, rowNum, colNum, cellValue); MessageBox.Show(s); }