This is a migrated thread and some comments may be shown as answers.

how to set row header text

8 Answers 1217 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Larry
Top achievements
Rank 1
Larry asked on 15 Nov 2012, 03:51 AM
Hi, i want to know how to set  row header text in datagird. usually, the icon arrow was displayed.

8 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Nov 2012, 08:35 AM
Hello Larry, 

Do you mean the Column Header?
Or what row header?

If you mean the column header you could just register for the DataBindingComplete event and then iterate trough the columns setting the column header you require.
foreach (var column in grid.Columns)
{
    column.HeaderText = "SomeHeader for field " + column.FieldName;
}

The FieldName in this case is the property bounded to the column.

If you have any other questions, please let me know.

Best Regards,
Emanuel Varga
WinForms MVP
0
Larry
Top achievements
Rank 1
answered on 15 Nov 2012, 09:22 AM
Hi,thanks for your response. i mean,row header not column header.

i want display row number in row header.  now in datagrid the selected row
will display a icon that seem like a left arrow.
0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Nov 2012, 09:37 AM
Hello,

Got it now, for this you should handle the ViewCellFormatting event and do something like this:
void grid_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    if (!(e.Row is GridViewDataRowInfo))
    {
        return;
    }
 
    if (e.CellElement is GridRowHeaderCellElement)
    {
        e.CellElement.Image = null;
        e.CellElement.Text = (grid.CurrentView.ViewInfo.Rows.IndexOf(e.Row)+1).ToString();
    }
}

If you have any other questions, please let me know.

Best Regards,
Emanuel Varga
WinForms MVP
0
Larry
Top achievements
Rank 1
answered on 16 Nov 2012, 02:12 AM
Hi,Thanks for your help. It is worked. But it can't auto change row header width, when content width is bigger than row header width, it is
display 1...    How can i  change row header width automaticly to display full row number.
0
Boo
Top achievements
Rank 2
answered on 16 Nov 2012, 05:50 AM
Hi Larry,

 I used this

 _MyRadGrid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;


Boo.
0
Plamen
Telerik team
answered on 19 Nov 2012, 04:12 PM
Hi guys,

Thank you for writing.

@Boo - the AutoSizeColumnsMode property will fill the entire grid area with the columns, however, it will not change the width of the row header, as required.

Currently, it is not possible to dynamically set the width of the row header cell. However, there is a property, which allows you to set the width of all row headers cells in the control at once:
radGridView1.TableElement.RowHeaderColumnWidth = 100;

As already suggested, you should use the ViewCellFormatting event to achieve your scenario. More information about the formatting abilities of the control can be found in this documentation article: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html.

Alternative way of accessing the RowIndex is to do it straight from the event arguments:
void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement is GridRowHeaderCellElement && e.Row is GridViewDataRowInfo)
    {
        e.CellElement.Image = null;
        e.CellElement.Text = (e.RowIndex + 1).ToString();
    }
}

I hope that you find the provided information useful. Should you have any other questions, I will be glad to assist you.

Greetings,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Larry
Top achievements
Rank 1
answered on 20 Nov 2012, 04:02 AM
Hi, that's a perfect answer. But i want to know how to calculate row header text width automaticly.For example, when row number is 12,the width is 20. when row number is 100 the width is 30. Could you tell me a way to calculate the text pixel width or point width ? 
0
Plamen
Telerik team
answered on 22 Nov 2012, 03:37 PM
Hello guys,

@Larry - Currently, the width of the row header cell does not support auto-resize. As I already suggested, you could use an alternative way:
radGridView1.TableElement.RowHeaderColumnWidth = 100;

A way to calculate the width of the text is:
e.CellElement.ColumnInfo.Width = Math.Max(e.CellElement.ColumnInfo.Width, newWidth);

Another way to calculate the text width is to use the Graphics.MeasureString Method or the Windows Forms static TextRenderer.MeasureText Method.

TextRenderer uses GDI to render the text, whereas Graphics uses GDI+. The two use a slightly different method for laying out text so the sizes are different.

Which one you should use depends on what will eventually be used to actually draw the text. If you are drawing it with GDI+ Graphics.DrawString, measure using Graphics.MeasureString. If you are drawing using GDI TextRenderer.DrawText, measure using TextRenderer.MeasureText.

Should you have any other questions, I will be glad to assist you.

Kind regards,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Larry
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Larry
Top achievements
Rank 1
Boo
Top achievements
Rank 2
Plamen
Telerik team
Share this question
or