Live Chat Icon For mobile
Live Chat Icon

How can I place text in the rowheader column of my datagrid

Platform: WinForms| Category: Datagrid

There is no text property exposed for a rowheader cell. But you can handle the Paint event and draw header text yourself. You can download sample projects (C#, VB) that illustrate one technique for doing so.

The sample loads the datagrid in the form’s Load event. In addition, this event is used to set the rowheaderwidth of the datagrid, and to remember the point where cell 0,0 is located. This point will allow us to find the toprow number when we need it to start drawing the header text. A handler for the datagrid’s Paint event is used to draw the text. It finds the toprow using the point from the original cell 0,0, and using the toprow determines the correct text for each rowheader. Finally, to avoid the complication of the user changing rowheights, we derive a new grid to prevent this.

private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  int row = TopRow();
  int yDelta = dataGrid1.GetCellBounds(row, 0).Height + 1;
  int y = dataGrid1.GetCellBounds(row, 0).Top + 2;
  CurrencyManager cm = (CurrencyManager) this.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
  while(y < dataGrid1.Height - yDelta && row < cm.Count)
    //get & draw the header text...
    string text = string.Format('row{0}', row);
    e.Graphics.DrawString(text, dataGrid1.Font, new SolidBrush(Color.Black), 12, y);
    y += yDelta;

Here is a datagrid with red row headers containing text.

Share with

Related FAQs

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

Please submit your question and answer.