Here are some frequently asked questions about Windows Forms and their answers.
Browse by Tags
All Tags » DataGridView (RSS)
-
|
When the user selects a row in the DataGridView and hits the delete key, the UserDeletingRow event fires. You can prompt the user if they want to continue deleting the row. It is recommended that you only do this if the row being deleted is not the new...
|
-
|
By default, toolbars and menus do not force validation for controls. Validation is a required part of validating and updating the data in a bound control. Once the form and all bound controls are validated, any current edits need to be committed. Finally...
|
-
|
By setting the DataGridViewColumn.SortMode property you can disable the ability for the user to sort on the given column. You can use Visual Studio 2005 to set this property by right-clicking on the DataGridView and choosing the Edit Columns option. Next...
|
-
|
The DataGridView does not provide any support for showing master-details data in the same DataGridView. The previously shipped Windows Forms DataGrid control can be a solution if this is something that you need.
|
-
|
One of the most common scenarios for using the DataGridView control is the master/detail form, in which a parent/child relationship between two database tables is displayed. Selecting rows in the master table causes the detail table to update with the...
|
-
|
The DataGridView does not provide any new features apart from virtual mode to enable this. What you can do is use the JoinView class described in the following article http://support.microsoft.com/default.aspx?scid=kb;en-us;325682 . Using this class you...
|
-
|
The data you display in the DataGridView control will normally come from a data source of some kind, but you might want to display a column of data that does not come from the data source. This kind of column is called an unbound column. Unbound columns...
|
-
|
Sometimes when using the error text and icon feature you want an immediate feedback to the user that something that they typed into a cell is incorrect. By default when setting the ErrorText property the error icon will not appear if the cell is in edit...
|
-
|
Sometimes data that you want to display in the DataGridView has a relationship between two tables such as a category and subcategory. You want to let the user select the category and then choose between a subcategory based upon the category. This is possible...
|
-
|
By default a DataGridViewComboBoxCell does not support typing into the cell. There are reasons though that typing into the combo box works well for your application. To enable this, two things have to be done. First the DropDownStyle property of the ComboBox...
|
-
|
By default the image column and cell convert null values to the standard "X" image. You can make no image show up by changing the column's NullValue property to null. The following code example sets the NullValue for an image column: this...
|
-
|
By default, text in a DataGridViewTextBoxCell does not wrap. This can be controlled via the WrapMode property on the cell style (e.g. DataGridView.DefaultCellStyle.WrapMode). Set the WrapMode property of a DataGridViewCellStyle to one of the DataGridViewTriState...
|
-
|
By setting the AutoSizeMode for the last column to Fill the column will size itself to fill in the remaining client area of the grid. Optionally you can set the last column's MinimumWidth if you want to keep the column from sizing too small.
|
-
|
Drag and dropping to reorder rows is not built into the DataGridView, but following standard drag and drop code you can easily add this functionality to the DataGridView. The code fragment below shows how you can accomplish this. It assumes that you have...
|
-
|
Sometimes it is helpful to know when a user has selected an item in the ComboBox editing control. With a ComboBox on your form you would normally handle the SelectedIndexChanged event. With the DataGridViewComboBox you can do the same thing by using the...
|
-
|
If you hook up an event handler on your editing control that is temporary (maybe for a specific cell in a specific column) you can remove the event handler in the CellEndEdit event. You can also remove any existing event handlers before adding an event...
|
-
|
Sometimes you will need to handle specific events provided by the editing control for a cell. You can do this by first handling the DataGridView.EditingControlShowing event. Next access the DataGridViewEditingControlShowingEventArgs.Control property to...
|
-
|
By default the DataGridView control does not provide sorting on multiple columns. Depending upon if the DataGridView is databound or not, you can provide additional support for sorting on multiple columns. Databound DataGridView When the DataGridView...
|
-
|
In the DataGridView control, text box columns use automatic sorting by default, while other column types are not sorted automatically. Sometimes you will want to override these defaults. In the DataGridView control, the SortMode property value of a column...
|
-
|
Sometimes you will want to display only some of the columns that are available in a DataGridView. For example, you might want to show an employee salary column to users with management credentials while hiding it from other users. To hide a column programmatically...
|
-
|
By default, text in a DataGridViewTextBoxCell does not wrap. This can be controlled via the WrapMode property on the cell style (e.g. DataGridView.DefaultCellStyle.WrapMode). Because text doesn't wrap, new line characters in the text do not apply...
|
-
|
The DataGridView control only supports displaying an actual control when a cell is in edit mode. The DataGridView control is not designed to display multiple controls or repeat a set of controls per row. The DataGridView control draws a representation...
|
-
|
By default the DataGridView's navigation model does not have any ability to restrict focus to a specific cell. You can implement your own navigation logic by overriding the appropriate keyboard, navigation and mouse methods such as DataGridView.OnKeyDown...
|
-
|
While a cell can be read-only to prevent it from being editable, the DataGridView does not have built-in support for disabling a cell. Normally the concept of "disabled" means that the user cannot navigate to it and usually has a visual cue...
|
-
|
The ReadOnly property indicates whether the data displayed by the cell can be edited. You can set ReadOnly for individual cells, or you can make an entire row or column of cells read-only by setting the DataGridViewRow.ReadOnly or DataGridViewColumn.ReadOnly...
|
-
|
I followed the instructions in the article How to: Host Controls in Windows Forms DataGridView Cells in the Visual Studio 2005 Library. I've used this to implement a MaskedTextBox control in a DataGridView. It seems to work but how do I make the "mask"...
|
-
|
I was wondering if it's possible to have a DataGridView list relational data in the same grid by branching the data as can be done in previous controls. It seems any relational data has to be viewed in a separate grid. Am I missing something? You...
|
-
|
I am using a DataGridViewComboBoxColumn in a DataGridView. When the DataSource of the DataGridViewComboBoxColumn has zero items from a SQL Select statement, all the whole will be a mash. How should I deal with this? This has been fixed in future versions...
|
-
|
In fact base.Clone() returns an object of type equals to this.GetType(). A reflection invocation on the default constructor might be done in the DataGridViewTextBox.Clone() method. It seems that the one methodology for implementing ICloneable would be...
|
-
|
I followed the article How to: Host Controls in Windows Forms DataGridView Cells and have inherited from DataGridViewTextBoxCell to create a custom cell for a DataGridView column. When the DataGridView needs to create a new cell in the column the method...
|
-
|
I have a ContextMenuStrip on a DataGridView with some action to perform on the selected record in the grid. My problem is that when the user right-clicks on a record, that record is not selected and he performs the action on an other record. The simple...
|
-
|
You can set a row's HeaderCell.Value property to a value and it will be painted. Alternatively - and fairly easy - handle the RowPostPaint and paint the row number in the header cell like so: Private Sub dataGridView1_RowPostPaint(ByVal sender As...
|
-
|
How do I add a control to a column header like a Button or TextBox? We don't have any built in support for this since column header cells do not have code to allow it to be editable. For a button you can simply custom paint the button by handling...
|
-
|
I wrote this to set the HeaderText in a DataGridView: dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = ds; dataGridView1.DataMember = ds.Tables[0].TableName; // change the HeaderText of columns. dataGridView1.Columns[0].HeaderText...
|