I have a GridView that displays data on three columns. On the third column I have a CheckBox. I have two things that I need to achieve
CheckBoxwill be checked based on the column value (1,0)- If it is checked the rest of the two columns should display
####However the data for the two columns should remain in the database.
How can this be achieved?
Can I find the CheckBox on RowDataBound event and check the value and make the CheckBox checked and unchecked? How about making the other columns ####?
NEW Comments:
string str = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[2].ToString();
this helps to set the checkbox checked or not.
if checked is true I am trying the following.
((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[0] = "#####";
((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[1] = "#####";
((System.Data.DataRowView)(e.Row.DataItem)).Row.AcceptChanges();
It is displaying the gridview checkbox as checked but the column value is not changed to “####”
You can turn your item columns into
TemplateColumnsand do the following which will localize your code to the control level and you don’t have to worry about all the searching. I pefer to never use the built in column types because there are usually future enhancements that require changing the columns toTemplateColumnsanyways. It also gives you a lot of flexibiltiy on useage.Here is an example:
Then in your codebehind implement each control’s
OnDataBinding:The advantages to doing it this way is your could replace code easily as it is all isolated and has no ‘searching’ for expected controls. I very rarely use the
RowDataBoundevent because it makes you have to write specific code to look for the controls and it makes more sense to me to have the code localized to the control. If someone changes something they know there are ONLY affecting that one control instead of everything on the row possibly as a side effect. You could also use the<%#method and do theEvals right in the markup but I personally prefer to never have any code in the aspx markup at all.