Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i try code as below:
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
dgvItem.FirstDisplayedScrollingRowIndex = vScrollBar1.Value;
}

I Get error as below :
"Specified argument was out of the range of valid values.
Parameter name: value "
how to fix this error. Please help me.
Thanks
Posted
Comments
BillWoodruff 10-Dec-14 2:52am    
Put a breakpoint in the EventHandler and write the values to the Console, and then analyze where the error occurred, and what the value was that caused the error. Then examine the range of possible values for DataGridView scrolling.
Cho Lay 10-Dec-14 3:04am    
When changed my code as below
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{

//dgvItem.FirstDisplayedScrollingRowIndex = vScrollBar1.Value;

if (dgvItem.FirstDisplayedScrollingRowIndex + dgvItem.DisplayedRowCount(false) < dgvItem.Rows.Count)
{
dgvItem.FirstDisplayedScrollingRowIndex += dgvItem.DisplayedRowCount(false);

}
else
{
dgvItem.FirstDisplayedScrollingRowIndex = dgvItem.Rows.Count - 1;
}

}
i have not error when scroll but scroll down is ok but scroll up not load datagried view.
what need my code for scroll up. please Give me instruction and Solution.

1 solution

As Bill says, the debugger is your best bet here - but I think you will find that the problem is pretty simple: The Value property of the VScrollBar is not automatically linked to the DataGridView, so it ranges between the Minimum and Maximum values, instead of being limited by the number of rows in the DGV. So when you scroll, you get a number between 0 and 100 (the defaults for Minimum and Maximum) and if your DGV has only ten row, you will get an error very quickly...

Set the Maximum property to the number of rows (minus one) and it should be fine.
 
Share this answer
 
Comments
Cho Lay 10-Dec-14 4:10am    
Yes i already write code like that :
public frmItemList()
{
InitializeComponent();


this.vScrollBar1.Minimum = 0;
this.vScrollBar1.Maximum = this.dgvItem.DisplayRectangle.Height;
this.vScrollBar1.LargeChange = vScrollBar1.Maximum / vScrollBar1.Height + this.dgvItem.Height -10 ;
this.vScrollBar1.SmallChange = 30;
this.vScrollBar1.Value = Math.Abs(this.dgvItem.AutoScrollOffset.Y);

}
private void frmItem_Load(object sender, EventArgs e)
{
dgvdatabind();
this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(vScrollBar1_Scroll);
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
dgvItem.FirstDisplayedScrollingRowIndex = vScrollBar1.Value;

//if (dgvItem.FirstDisplayedScrollingRowIndex + dgvItem.DisplayedRowCount(false) < dgvItem.Rows.Count)
//{
// dgvItem.FirstDisplayedScrollingRowIndex += dgvItem.DisplayedRowCount(false);

//}
//else
//{
// dgvItem.FirstDisplayedScrollingRowIndex = dgvItem.Rows.Count - 1;
//}

}


but i get error when i scroll vScrollBar.
I don't know what wrong and what need my code :(
OriginalGriff 10-Dec-14 4:24am    
No, DisplayRectangle.Height is in *pixels* not in *rows*.
But the FirstDisplayedScrollingRowIndex is in rows not pixels - and since there are 20 or 25 pixels to a DGV row... :laugh:
You need the number of rows in the DGV (which will depend on your source), not the pixel height.
Cho Lay 10-Dec-14 4:34am    
if not I have no idea more. So please edit my code.Please help me. Thanks
Cho Lay 10-Dec-14 4:40am    
when i changed like that
this.vScrollBar1.Maximum = this.dgvItem.Rows.Count ;

but not ok because vScroll and DGV are fit so i can't scroll :)
Cho Lay 10-Dec-14 5:06am    
I am now ok thanks :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900