EDN Admin
Well-known member
Hi there,
heres a very simple example of how to produce a minor bug and eventually an unhandled exception with the DataGridView component.
<span style="font-weight:bold Scenario: I want to add rows to a DataGridView manually, one row at a time as a result after long-running operations. Each time, before a row is added, the DataGridView is disabled and re-enabled after the row has been added (actually whole parts of my form are disabled so the user cannot interact when a row is currently added, and consequently this also disables the DataGridView).
<span style="font-weight:bold Bug: After several rows have been added, the scrollbar is not updated correctly. You cannot scroll through all entries using the scrollbar.
<span style="font-weight:bold Unhandled Exception<span style="font-weight:bold : If you use the keys to scroll through the rows, you receive a ArgumentOutOfRangeException.
<span style="font-weight:bold Example:
Create a new Windows application
Add a DataGridView and a Button component
Add the following code to the Forms constructor (this just ensures there will be a scrollbar):
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
public Form1()
{
InitializeComponent();
dataGridView1.Height = 100;
}
Add an event handler for the click event of the button and then add the following code to simulate the scenario:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Columns.Add("Column1", "HeaderText1");
for (int i = 0; i < 10; i++)
{
dataGridView1.Enabled = false;
dataGridView1.Rows.Add("Row " + (i + 1));
dataGridView1.Enabled = true;
}
}
Run the application
Click the button
Confirm the scrollbar is not updated properly. You cannot scroll to the end of the rows list using the scrollbar (I can scroll to half of "Row 6").
Focus the DataGridView (click a cell).
Use the down arrow key to scroll further down.
When youre about to reach "Row 9", you receive the following unhandled ArgumentOutOfRangeException in Visual Studio:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
Value of 132 is not valid for Value. Value should be between minimum and maximum.
Parameter name: Value
This can be reproduced using VS 2005 (8.0.50727.762 SP.050727-7600) with .NET 2.0.50727 SP1 as well as using VS 2008 (9.0.21022.8 RTM) with .NET 3.5.
<div style="text-align:left This seems to be a similar error like reported <a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=8589&SiteID=1 http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=8589&SiteID=1 here but in a different scenario.
View the full article
heres a very simple example of how to produce a minor bug and eventually an unhandled exception with the DataGridView component.
<span style="font-weight:bold Scenario: I want to add rows to a DataGridView manually, one row at a time as a result after long-running operations. Each time, before a row is added, the DataGridView is disabled and re-enabled after the row has been added (actually whole parts of my form are disabled so the user cannot interact when a row is currently added, and consequently this also disables the DataGridView).
<span style="font-weight:bold Bug: After several rows have been added, the scrollbar is not updated correctly. You cannot scroll through all entries using the scrollbar.
<span style="font-weight:bold Unhandled Exception<span style="font-weight:bold : If you use the keys to scroll through the rows, you receive a ArgumentOutOfRangeException.
<span style="font-weight:bold Example:
Create a new Windows application
Add a DataGridView and a Button component
Add the following code to the Forms constructor (this just ensures there will be a scrollbar):
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
public Form1()
{
InitializeComponent();
dataGridView1.Height = 100;
}
Add an event handler for the click event of the button and then add the following code to simulate the scenario:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Columns.Add("Column1", "HeaderText1");
for (int i = 0; i < 10; i++)
{
dataGridView1.Enabled = false;
dataGridView1.Rows.Add("Row " + (i + 1));
dataGridView1.Enabled = true;
}
}
Run the application
Click the button
Confirm the scrollbar is not updated properly. You cannot scroll to the end of the rows list using the scrollbar (I can scroll to half of "Row 6").
Focus the DataGridView (click a cell).
Use the down arrow key to scroll further down.
When youre about to reach "Row 9", you receive the following unhandled ArgumentOutOfRangeException in Visual Studio:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
Value of 132 is not valid for Value. Value should be between minimum and maximum.
Parameter name: Value
This can be reproduced using VS 2005 (8.0.50727.762 SP.050727-7600) with .NET 2.0.50727 SP1 as well as using VS 2008 (9.0.21022.8 RTM) with .NET 3.5.
<div style="text-align:left This seems to be a similar error like reported <a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=8589&SiteID=1 http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=8589&SiteID=1 here but in a different scenario.
View the full article