Skip to main content Skip to footer

Make ReadOnly cells scrollable in C1FlexGrid

Background:

How to make ReadOnly cells scrollable in C1FlexGrid.

Steps to Complete:

You can make normal cells scrollable by taking it as a textbox at the time of Editing and make that textbox scrollable but what if the cells are non-editable. Below is given the trick to achieve the same when cells are non-editable:

1. Create a TextBox and add it to the child control of the grid.

private TextBox textBoxForDisplay;
  this.textBoxForDisplay = new TextBox();
  this.textBoxForDisplay.ScrollBars = ScrollBars.Both;
   this.textBoxForDisplay.Multiline = true;
   //Make it readonly:
   this.textBoxForDisplay.ReadOnly = true;
   this.textBoxForDisplay.Visible = false;
   this.c1FlexGrid1.Controls.Add(this.textBoxForDisplay);

2. Handle the AfterSelChange event of FlexGrid to show the TextBox on the selected ReadOnly cell.

private void C1FlexGrid1_AfterSelChange(object sender, C1.Win.C1FlexGrid.RangeEventArgs e)
     {
            if (e.NewRange.LeftCol == 2)
             {
             this.textBoxForDisplay.Visible = true;
           //Position of cell:
             Rectangle rectCell = this.c1FlexGrid1.GetCellRect(e.NewRange.TopRow,      
            e.NewRange.LeftCol);
           this.textBoxForDisplay.Location = rectCell.Location;
           this.textBoxForDisplay.Size = rectCell.Size;
          //Write Text to textbox:
          this.textBoxForDisplay.Text = (string)this.c1FlexGrid1[e.NewRange.TopRow,  
          e.NewRange.LeftCol];
          }
      else
      {
        this.textBoxForDisplay.Visible = false;
      }
  }

3. In the GotFocus event of TextBox, hide the blinking caret in the ReadOnly textbox cell as follows.

using System.Runtime.InteropServices;
[DllImport("user32")]
private static extern bool HideCaret(IntPtr hWnd);
private void TextBoxForDisplay_GotFocus(object sender, EventArgs e)
{
   if (textBoxForDisplay.ReadOnly == true)
         {
              HideCaret(textBoxForDisplay.Handle);
          }
  }

Prabhat Sharma