Adjusting DataGridView row heights on existing rows


M

michael sorens

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.RowTemplate.Height += 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
CustomDataGridView 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 CustomDataGridView : DataGridView
{
. . .
if (e.Control && e.KeyCode == Keys.Up)
{
Console.WriteLine("got ctrl-Up");
RowTemplate.Height += 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!
 
Ad

Advertisements

I

Ignacio Machin ( .NET/ C# MVP )

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.RowTemplate.Height += 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
CustomDataGridView 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 CustomDataGridView : DataGridView
{
    . . .
    if (e.Control && e.KeyCode == Keys.Up)
    {
          Console.WriteLine("got ctrl-Up");
          RowTemplate.Height += 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?
 
M

michael sorens

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.WriteLine in my code fragment (which prints as expected).
 
L

Linda Liu[MSFT]

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 DataGridViewRows that are created after
this property is changed.

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

foreach(DataGridViewRow row in this.dataGridView1.Rows)
{
row.Height += 1;
}
DataGridViewRow templateRow = this.dataGridView1.RowTemplate;
templateRow.Height += 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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

michael sorens

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.Height 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!
 
Ad

Advertisements

L

Linda Liu[MSFT]

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:
(e-mail address removed).

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

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top