1

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

  1. CheckBox will be checked based on the column value (1,0)
  2. 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 "####"

1
  • The column values are changed when you use e.Row.Cells[0].Text = "####"; Commented Sep 13, 2011 at 17:28

1 Answer 1

0

You can turn your item columns into TemplateColumns and 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 to TemplateColumns anyways. It also gives you a lot of flexibiltiy on useage.

Here is an example:

<asp:GridView ID="grdYourGrid" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="YourField1">
            <ItemTemplate>
                <asp:Literal runat="server" ID="ltYourField1" 
                    OnDataBinding="ltYourField1_DataBinding" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="YourField2">
            <ItemTemplate>
                <asp:Literal runat="server" ID="ltYourField2" 
                    OnDataBinding="ltYourField2_DataBinding" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="YourCheckBoxField">
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="chkYourCheckBoxField" 
                    OnDataBinding="chkYourCheckBoxField_DataBinding" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Then in your codebehind implement each control's OnDataBinding:

protected void ltYourField1_DataBinding(object sender, System.EventArgs e)
{
    Literal lt = (Literal)(sender);
    lt.Text = (bool)(Eval("YourCheckBoxField")) ?
        "##########" : Eval("YourField1");
}

protected void ltYourField2_DataBinding(object sender, System.EventArgs e)
{
    Literal lt = (Literal)(sender);
    lt.Text = (bool)(Eval("YourCheckBoxField")) ?
        "##########" : Eval("YourField2");
}

protected void chkYourCheckBoxField_DataBinding(object sender, System.EventArgs e)
{
    CheckBox chk = (CheckBox)(sender);
    chk.Checked = (bool)(Eval("YourCheckBoxField"));
}

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 RowDataBound event 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 the Evals right in the markup but I personally prefer to never have any code in the aspx markup at all.

Sign up to request clarification or add additional context in comments.

5 Comments

I like your approach! But, I have asp:boundfields and I wanted a way to implement it that way.
@kalls you can do it with a BoundField with very similar code (just use the DataBinding code I have included in your OnRowDataBound) but you will end up having to search for the controls. It's harder to maintain in the future for the next guy so that is why I avoid it.
Thanks Kelsey! I have some issues. I will post the code later on if I am not able to solve it. I sincerely appreciate your support.
Kelsey, I have added code to the above question. I appreciate your support. Once again Thank you!
Kelsey, I have selected your answer. It explains 2 ways of achieving the solution. I went with RowDataBound and based on the check box values I am using e.Row.Cells[0].Text = "####"; to replace the displayed value on the grid. Once again thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.