Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

Browse by Tags

All Tags » DataGridView » Using Controls (RSS)

Sorry, but there are no more tags available to filter with.

  • How do I display a confirmation dialog when the user tries to delete a row?

    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...
  • How do I commit the data to the database when clicking on a toolstrip button?

    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...
  • How do I prevent sorting?

    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...
  • How do I show master-details in the same DataGridView?

    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.
  • How do I show master-details?

    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...
  • How do I show data that comes from two tables?

    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...
  • How do I show unbound data along with bound data?

    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...
  • How do I show the error icon when the user is editing the cell?

    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...
  • How do I have a combo box column display a sub set of data based upon the value of a different combo box column?

    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...
  • How do I enable typing in the combo box cell?

    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...
  • How do I make the image column not show any images?

    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...
  • How do I have the cell text wrap?

    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...
  • How do I make the last column wide enough to occupy all the remaining client area of the grid?

    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.
  • How do I perform drag and drop reorder of rows?

    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...
  • How do I handle the SelectedIndexChanged event?

    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...
  • When should I remove event handlers from the editing control?

    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...
  • How do I hook up events on the editing control?

    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...
  • How do I sort on multiple columns?

    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...
  • How do I prevent the user from sorting on a column?

    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...
  • How do I hide 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...
  • Why does the cell text show up with "square" characters where they should be new lines?

    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...
  • How do I show controls in all cells regardless of edit?

    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...
  • How do I restrict user from setting focus to a specific cell?

    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...
  • How do I disable a cell?

    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...
  • How do I prevent a particular cell from being editable?

    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...
  • How do I provide a Mask property to the user for a MaskedTextBox control in a DataGridView column?

    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"...
  • Can I show relational data in a DataGridView?

    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...
  • Why doesn't DataGridViewComboBoxColumn work if the DataSource has zero items?

    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...
  • Is there another methodology for cloning the cells when you inherit?

    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...
  • How do I override the DataGridViewTextBox.Clone method? In general, how do I implement ICloneable in a derived class?

    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...
  • How do I make right-clicking on the DataGridView select the record or cell?

    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...
  • How do I add text to the row header, e.g., a row number, in a DataGridView?

    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 DataGridView column header?

    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...
  • How do I set the column HeaderText in a DataGridView?

    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...


Page view counter