Adjusting DataGridView row heights on existing rows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?=

    Adjusting DataGridView row heights on existing rows

    I want to be able to increase or decrease row heights of a populated
    DataGridView from the keyboard. I set up a test program with menu items to
    increase and decrease, assigned shortkey keys (ctrl-UpArrow and
    ctrl-DnArrow), and attached handlers that execute this code:

    public partial class MainForm : Form
    {
    . . .
    dataGridView.Ro wTemplate.Heigh t += increment;
    Refresh();
    . . .
    }

    I was surprised that this works reliably to change existing row heights
    because a web search seemed to indicate that would only work for rows created
    after that setting was done.

    My real goal, though, is to internalize this functionality into my own
    CustomDataGridV iew that derives from DataGridView. I worked out the
    appropriate key activations so that the DataGridView would correctly listen
    and respond to ctrl-UpArrow and ctrl-DnArrow. So I am certain it is executing
    the same code effectively:

    partial class CustomDataGridV iew : DataGridView
    {
    . . .
    if (e.Control && e.KeyCode == Keys.Up)
    {
    Console.WriteLi ne("got ctrl-Up");
    RowTemplate.Hei ght += 1;
    Refresh();
    }
    . . .
    }

    But in this test program, the rows do not change height. (I even tried
    Parent.Refresh( ); to make the code identical to the previous test but it
    still fails.)

    So:
    (1) Should the first test scenario work?
    (2) Why does the second test scenario fail?
    (3) How do I get the second scenario to work?

    Thanks!
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Adjusting DataGridView row heights on existing rows

    On Jun 12, 1:31 pm, michael sorens <m_j_sor...@new sgroup.nospam>
    wrote:
    I want to be able to increase or decrease row heights of a populated
    DataGridView from the keyboard. I set up a test program with menu items to
    increase and decrease, assigned shortkey keys (ctrl-UpArrow and
    ctrl-DnArrow), and attached handlers that execute this code:
    >
    public partial class MainForm : Form
    {
        . . .
        dataGridView.Ro wTemplate.Heigh t += increment;
        Refresh();
        . . .
    >
    }
    >
    I was surprised that this works reliably to change existing row heights
    because a web search seemed to indicate that would only work for rows created
    after that setting was done.
    >
    My real goal, though, is to internalize this functionality into my own
    CustomDataGridV iew that derives from DataGridView. I worked out the
    appropriate key activations so that the DataGridView would correctly listen
    and respond to ctrl-UpArrow and ctrl-DnArrow. So I am certain it is executing
    the same code effectively:
    >
    partial class CustomDataGridV iew : DataGridView
    {
        . . .
        if (e.Control && e.KeyCode == Keys.Up)
        {
              Console.WriteLi ne("got ctrl-Up");
              RowTemplate.Hei ght += 1;
              Refresh();
        }
        . . .
    >
    }
    >
    But in this test program, the rows do not change height. (I even tried
    Parent.Refresh( ); to make the code identical to the previous test but it
    still fails.)
    >
    So:
    (1) Should the first test scenario work?
    (2) Why does the second test scenario fail?
    (3) How do I get the second scenario to work?
    >
    Thanks!
    R u sure your datagrid has focus?
    and that it's receiving the keyDown?

    Comment

    • =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?=

      #3
      Re: Adjusting DataGridView row heights on existing rows

      As I stated: "I worked out the appropriate key activations so that the
      DataGridView would correctly listen and respond to ctrl-UpArrow and
      ctrl-DnArrow. So I am certain it is executing the same code effectively."
      Note the Console.WriteLi ne in my code fragment (which prints as expected).

      Comment

      • Linda Liu[MSFT]

        #4
        Re: Adjusting DataGridView row heights on existing rows

        Hi Michael,

        I performed a test based on your description but didn't reproduce the
        problem on my side.

        In my test, whether I change the TemplateRow property of the DataGridView
        in the handler of a menu item's Click event or in a derived DataGridView,
        the new settings only affect those DataGridViewRow s that are created after
        this property is changed.

        To get what you want, I suggest that you enumerate through the existing
        DataGridViewRow s and change the Height property of them manually and then
        change the RowTemplate's Height property for new DataGridViewRow s. For
        example:

        foreach(DataGri dViewRow row in this.dataGridVi ew1.Rows)
        {
        row.Height += 1;
        }
        DataGridViewRow templateRow = this.dataGridVi ew1.RowTemplate ;
        templateRow.Hei ght += 1;

        Please try my suggestion and let me know the result.

        Sincerely,
        Linda Liu
        Microsoft Online Community Support

        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        Gain technical skills through documentation and training, earn certifications and connect with the community

        ications.

        Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
        where an initial response from the community or a Microsoft Support
        Engineer within 1 business day is acceptable. Please note that each follow
        up response may take approximately 2 business days as the support
        professional working with you may need further investigation to reach the
        most efficient resolution. The offering is not appropriate for situations
        that require urgent, real-time or phone-based interactions or complex
        project analysis and dump analysis issues. Issues of this nature are best
        handled working with a dedicated Microsoft Support Engineer by contacting
        Microsoft Customer Support Services (CSS) at
        http://msdn.microsoft.com/subscripti...t/default.aspx.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.


        Comment

        • =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?=

          #5
          Re: Adjusting DataGridView row heights on existing rows

          Thanks, Linda. After you confirmed what I thought was the defined behavior, I
          went back and looked at my code again (written quite a while ago). I found my
          mistake: besides setting the RowTemplate.Hei ght on the key event, I also set
          the row height on the cell formatting event, which is why it worked for me
          :-) Silly oversight on my part. That is, as it turns out, a lot more
          practical solution in terms of response time than looping through all rows as
          you suggest, because I am dealing with over 100,000 rows on occasion. Thanks
          for spurring me to solve the riddle!

          Comment

          • Linda Liu[MSFT]

            #6
            Re: Adjusting DataGridView row heights on existing rows

            Hi Michael,

            Thank you for your feedback! I'm glad to hear that the problem is solved
            now.

            Yes, I also think handing the CellFormatting event of the DataGridView is
            better than looping through all DataGridView rows to get what you want.

            If you have any other questions in the future, please don't hesitate to
            contact us. It's always our pleasure to be of assistance!

            Have a good day!

            Sincerely,
            Linda Liu
            Microsoft Online Community Support

            Delighting our customers is our #1 priority. We welcome your comments and
            suggestions about how we can improve the support we provide to you. Please
            feel free to let my manager know what you think of the level of service
            provided. You can send feedback directly to my manager at:
            msdnmg@microsof t.com.

            This posting is provided "AS IS" with no warranties, and confers no rights.


            Comment

            Working...