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 get the selected text in the active cell of a DataGrid?

You need to get the DataGridTextBoxColumn.TextBox member, and retrieve the SelectedText from it for the active cell. The sample shows how you can do this as part of handling this TextBox's rightClick. This code assumes you have specifically added DataGridTextBoxColumn for each column style. You can also download a VB sample.

private void HandleMouseDown( object sender, MouseEventArgs e )
{
  if ( e.Button == MouseButtons.Right )
  {
    DataGridTableStyle ts = dataGrid1.TableStyles[ "customers" ];
    DataGridTextBoxColumn cs = (DataGridTextBoxColumn)
      ts.GridColumnStyles[ dataGrid1.CurrentCell.ColumnNumber ];
    MessageBox.Show( "Selected: " + cs.TextBox.SelectedText );
  }
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter