how to set row header text

9 posts, 0 answers
  1. Larry
    Larry avatar
    4 posts
    Member since:
    Jul 2012

    Posted 14 Nov 2012 Link to this post

    Hi, i want to know how to set  row header text in datagird. usually, the icon arrow was displayed.
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Nov 2012 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Larry
    Larry avatar
    4 posts
    Member since:
    Jul 2012

    Posted 15 Nov 2012 Link to this post

    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.
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Nov 2012 Link to this post

    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
  6. Larry
    Larry avatar
    4 posts
    Member since:
    Jul 2012

    Posted 15 Nov 2012 Link to this post

    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.
  7. Boo
    Boo avatar
    5 posts
    Member since:
    Jul 2012

    Posted 15 Nov 2012 Link to this post

    Hi Larry,

     I used this

     _MyRadGrid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;


    Boo.
  8. Plamen
    Admin
    Plamen avatar
    132 posts

    Posted 19 Nov 2012 Link to this post

    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.
  9. Larry
    Larry avatar
    4 posts
    Member since:
    Jul 2012

    Posted 19 Nov 2012 Link to this post

    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 ? 
  10. Plamen
    Admin
    Plamen avatar
    132 posts

    Posted 22 Nov 2012 Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready