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 a DataGrid's column header text in a MouseUp event?

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

// ...

private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
{
  Point point = new Point( e.X, e.Y );
  DataGrid.HitTestInfo hti = dataGrid1.HitTest( point );
  if ( hti.Type == DataGrid.HitTestType.ColumnHeader )
  {
    CurrencyManager cm =
      (CurrencyManager) BindingContext[ dataGrid1.DataSource ];
    DataView dataView = (DataView) cm.List;
    string columnHeader = dataView.Table.Columns[ hti.Column ].ToString();

    // process columnHeader...
    MessageBox.Show( columnHeader );
  }
}

George Shepherd, Syncfusion, and Stuart Celarier, Fern Creek



Page view counter