winforms - ArgumentOutOfRange Exception in c# -


i'm trying access column name of selected row in datagridview control in windows form, i'm getting argumenoutofrange exception of following code:

messagebox.show(datagridview1.selectedcolumns[datagridview1.currentcell.columnindex].tostring()); 

producing:

argumentoutofrangeexception unhandled  index out of range. must non-negative , less size of collection. parameter name: index 

could please tell me how can overcome error?

the datagridview.selectedcolumns collection separate collection datagridview.columns , can have different set (a subset) of columns.

for example if have 5 column view, , third , fourth selected,

datagridview.selectedcolumns.count == 2 datagridview.columns.count == 5 

and of using fourth column (index of 3) code becomes

datagridview.selectedcolumns[3] 

which blows (rightly) indexoutofbounds.

to sum up, in case, should using columns property, , not selectedcolumns.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -