Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am having checkbox inside listview and i am binding data to that listview from database.
what i want now is when the data is bind to the listview i want the checkbox to be checked
.for that i try to refer the checkbox in the button event and make the checkbox checked but
it will display an error message null reference exception object reference not set to instance
of an object

here is my code in the button click event

C#
CheckBox myTextbox = GetTemplateChild("chb_standard") as CheckBox;
                  
                        myTextbox.IsChecked = true;
                    
if any one have any idea help  me thanks in advance.
Posted

1 solution

Would be easier to bind the IsChecked property to a public property to handle this situation.

For example:

C#
private bool _cbIsChecked;


C#
public bool CbIsChecked
{
     get{return _cbIsChecked;}
     set{_cbIsChecked = value;}
}


Then you should define a method where this property is been set.

Don't forget to implement the INotifyPropertyChanged, so the UI refreshes itself.
Raise it in the set of the public property.

For any further questions, just ask.

Hope it helps :)
 
Share this answer
 
Comments
prabhakar78 6-Sep-13 2:46am    
thanks for your help . my problem is once the items is bind to the listview from database
i want the checkbox in the listview to be checked i am write it in the code like this


for (int k = 0; k <= listView1.Items.Count - 1; k++)
{
ListViewData lvc = (ListViewData)listView1.Items[k];

CheckBox myTextbox = GetTemplateChild("chb_standard") as CheckBox;
mytextbox.ischecked =true;

i did exactly like this but it will display objectnullreferenceexecption if any idea help me



}
Marvin Ma 6-Sep-13 2:57am    
For understanding: You have a text box in the column header and want it to be checked if the data from database were loaded succesfully?

Let me know if I'm right.
prabhakar78 6-Sep-13 3:05am    
yes you are right .but i want it to do it in coding for some reason.
Marvin Ma 6-Sep-13 3:14am    
Is there a check box in each row of the table?

prabhakar78 6-Sep-13 3:17am    
yes

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