Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

How to dynamically change Datafield of a Datagrid's bound column tag in asp.net?

Thanks in advance :-)

What I have tried:

i have tried,
(BoundField)datagrid.Columns[0]).DataField = "TestID";
but im getting 'cannot convert system.web.ui.webcontrol.datagridcontrol to system.web.ui.webcontrol.boundfield'
Posted
Updated 6-Sep-16 4:37am
v2
Comments
Richard Deeming 6-Sep-16 8:25am    
The code you've posted won't compile - you're missing an opening bracket.

1 solution

The reason for that exception is because you are not correctly casting the DataField property.

You need
C#
((BoundField)datagrid.Columns[0]).DataField = "TestID";

or
C#
BoundField col1 = datagrid.Columns[0] as BoundField;
col1.DataField = "TestID";
 
Share this answer
 

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