Live Chat Icon For mobile
Live Chat Icon

How can I make my grid never have an active edit cell and always select whole rows (as in a browser-type grid)

Platform: WinForms| Category: Datagrid

TFor a single row select datagrid, you can get both these behaviors by using a custom column style and overriding its Edit method. In your override, handle unselecting and
selecting the current row, and DO NOT call the base class. Not calling the base class keeps the cell from becoming active. Here is a code snippet suggesting how this might be done. You can download a full working project (CS, VB).

public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
{
  private int SelectedRow = -1;
  protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly,string instantText,bool cellIsVisible)
  {
    //make sure previous selection is valid
    if(SelectedRow > -1 && SelectedRow < source.List.Count + 1)
      this.DataGridTableStyle.DataGrid.UnSelect(SelectedRow);
    SelectedRow = rowNum;
    this.DataGridTableStyle.DataGrid.Select(SelectedRow);
  }
}

If you want to handle multi-selections, then there is more work to be done. One solution is to still override Edit as above, but have an empty implementation. Do not have the code the handles the selected row and do not call the baseclass. To handle the selections in this case, subclass the datagrid and override its OnMouseDown virtual method to change all cell clicks into row header clicks. Also override OnCurrentCellChanged to handle moving the current cell with the keyboard. You can download a sample (C#, VB) that implements this functionality.

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.