Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

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

Windows Forms FAQs

How do I restrict the keystrokes that are accepted in a DataGrid column?

The sample project DataGridRestrictedInput illustrates how to restrict a column to only accept digits. The technique is general and you can apply it to other kinds of restricted input. Here's the basic idea.

You create a custom column style that derives from DataGridTextBoxColumn and handle the KeyPress event of its TextBox member.

using System;
using System.Windows.Forms;

public class DataGridDigitsTextBoxColumn : DataGridTextBoxColumn
{
  public DataGridDigitsTextBoxColumn()
  {
    TextBox.KeyPress += new KeyPressEventHandler( TextBox_KeyPress );
  }

  protected override void Dispose( bool disposing )
  {
    if ( disposing )
      TextBox.KeyPress -= new KeyPressEventHandler( TextBox_KeyPress );
    base.Dispose( disposing );
  }

  private void TextBox_KeyPress( object sender, KeyPressEventArgs e )
  {
    // ignore if not digit or control key
    if ( !( char.IsDigit( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
      e.Handled = true;
  }
}

The fragment below illustrates how to use the custom column style. A DataSet is filled with the Products table from the Northwind database, and a DataGrid is bound to the data source as usual. Then a DataGridTableStyle is created, DataGridColumnStyle objects are added to the DataGridTableStyle.GridColumnStyles collection, and then the table style is added to the DataGrid.TableStyles collection. In this example, two columns are displayed in the DataGrid, the second of which uses the custom column style so it only accepts digit keystrokes.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

public class Form1 : Form
{
  private DataGrid dataGrid1;
  private Container components = null;

  private const string connectionString =
    @"Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=(local);";
  private const string commandText = "SELECT * FROM Products";
  private const string tableName = "Table"; // default DataTable name

  // ...

  private void Form1_Load(object sender, System.EventArgs e)
  {
    // create data source
    DataSet dataSet = new DataSet();
    using ( SqlDataAdapter adapter =
        new SqlDataAdapter( commandText, connectionString ) )
      adapter.Fill( dataSet );

    // bind DataGrid to data source
    dataGrid1.DataSource = dataSet.Tables[ tableName ];

    // add custom table style
    AddTableStyle( dataGrid1 );
  }

  private void AddTableStyle( DataGrid dataGrid )
  {
    DataGridTableStyle table = new DataGridTableStyle();
    table.MappingName = tableName;

    // regular text box column
    DataGridColumnStyle productNameColumn = new DataGridTextBoxColumn();
    AddColumnStyle( table, productNameColumn, "ProductName",  "Product" );

    // digits-only text box column
    DataGridColumnStyle unitsInStockColumn =
      new DataGridDigitsTextBoxColumn();
    AddColumnStyle( table, unitsInStockColumn, "UnitsInStock",  "Units in Stock" );

    dataGrid.TableStyles.Add( table );
  }

  private void AddColumnStyle( DataGridTableStyle table,
    DataGridColumnStyle column, string mappingName, string headerText )
  {
    column.MappingName = mappingName;
    column.HeaderText = headerText;
    table.GridColumnStyles.Add( column );
  }

George Shepherd, Syncfusion; and Stuart Celarier, Fern Creek



Page view counter