none
Can I display number of row on RowHeaders in the DataGridView? RRS feed

  • Question

  • hi

    my datagridview is right to left.

    when i use this code , numbers show on last column.

    when datagridview is left to right this code is correct.

    I want to display number of row and image on all RowHeader of DataGridView;

    private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {

    using (SolidBrush b = new SolidBrush(DataGridView1.RowHeadersDefaultCellStyle.ForeColor)) { e.Graphics.DrawString(e.RowIndex.ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4); }

    }




    • Edited by Neda.IT Monday, June 9, 2014 4:55 AM
    • Moved by Caillen Tuesday, June 10, 2014 1:55 AM
    Monday, June 9, 2014 4:53 AM

Answers

  • Try adding an if statement. If the datagridview is right to left, measure the width of the string first.

    using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
    {
        if (dataGridView1.RightToLeft == RightToLeft.No)
        {
            e.Graphics.DrawString(e.RowIndex.ToString(), e.InheritedRowStyle.Font, b,
                e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        else
        {
            string index = e.RowIndex.ToString();
            var size = e.Graphics.MeasureString(index, e.InheritedRowStyle.Font);
            e.Graphics.DrawString(index, e.InheritedRowStyle.Font, b, e.RowBounds.Right - 20 - size.Width,
                e.RowBounds.Location.Y + 4);
        }
    }
    
    Best regards,

    • Marked as answer by Neda.IT Saturday, June 14, 2014 3:36 AM
    Wednesday, June 11, 2014 12:58 PM
    Moderator

All replies

  • Hi Neda.IT,

    I'm moving your question to Windows Forms forum where more engineers will give you professional help.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 10, 2014 1:58 AM
  • thanks

    • Edited by Neda.IT Tuesday, June 10, 2014 3:35 AM
    Tuesday, June 10, 2014 3:35 AM
  • Try adding an if statement. If the datagridview is right to left, measure the width of the string first.

    using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
    {
        if (dataGridView1.RightToLeft == RightToLeft.No)
        {
            e.Graphics.DrawString(e.RowIndex.ToString(), e.InheritedRowStyle.Font, b,
                e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        else
        {
            string index = e.RowIndex.ToString();
            var size = e.Graphics.MeasureString(index, e.InheritedRowStyle.Font);
            e.Graphics.DrawString(index, e.InheritedRowStyle.Font, b, e.RowBounds.Right - 20 - size.Width,
                e.RowBounds.Location.Y + 4);
        }
    }
    
    Best regards,

    • Marked as answer by Neda.IT Saturday, June 14, 2014 3:36 AM
    Wednesday, June 11, 2014 12:58 PM
    Moderator
  • thanks
    i display image in row header
    now i wanna change image on mouse hover and after clicked
    please guide me
    thanks a lot
    • Edited by Neda.IT Saturday, June 14, 2014 5:02 AM
    Saturday, June 14, 2014 3:46 AM