2
<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false">
                        <Columns>
                            <asp:BoundField DataField="Q" HeaderText="Q" />
                            <asp:TemplateField HeaderText="C">
                                <ItemTemplate>
                                    <asp:CheckBox ID="CCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="CCheckbox_CheckedChanged" />
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:TemplateField HeaderText="R">
                                <ItemTemplate>
                                    <asp:CheckBox ID="RCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="RCheckbox_CheckedChanged" />
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:TemplateField HeaderText="E">
                                <ItemTemplate>
                                    <asp:CheckBox ID="ECheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="ECheckbox_CheckedChanged" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

This is my Aspx code for the GridView

if (ViewState["SearchGrid"] == null)
            {
                SearchTable.Columns.Add(new DataColumn("Q", typeof(string)));
                SearchTable.Columns.Add(new DataColumn("C", typeof(bool)));
                SearchTable.Columns.Add(new DataColumn("R", typeof(bool)));
                SearchTable.Columns.Add(new DataColumn("E", typeof(bool)));
                ViewState["SearchGrid"] = SearchTable;
            }
            SearchTable =(DataTable) ViewState["SearchGrid"];
            DataRow dr = null;
            dr = SearchTable.NewRow();
            dr["Q"] = MySearchTextBox.Text;
            switch (SearchType)
            {
                case "Contains": dr["C"] =true;
                    break;
                case "Related": dr["R"] = true;
                    break;
                case "Exact": dr["E"] = true;
                    break;
            }

and my code behind.

At the time of adding the row I have to check a paticular check box. I have been trying to check the checkbox by giving its values as true but it does not seem to work. Can someone tell me what I am doing wrong here?

5
  • Are you setting the CheckBox to true or CheckBox.Checked to true? Commented Sep 6, 2013 at 16:25
  • @garrison how can i put the checkBox.checked = true? because the control has not been added to table, so that i can find the control and check it? Commented Sep 6, 2013 at 16:28
  • 3
    Handle the GridView's OnRowDataBound event. Find the CheckBox, set its Checked value based on your algorithm. Commented Sep 6, 2013 at 16:30
  • Where are you calling your c# code? inside CheckedChanged event handler? Commented Sep 6, 2013 at 17:24
  • hi garrison, yes i did it that way and the problem got solved but my anothe problem is after adding another row, the previous row Checkboxes gets unchecked, i enabled view state for check boxes but still the pervious rows checkboxes gets unchecked. any solution for this problem? Commented Sep 6, 2013 at 17:26

2 Answers 2

1

You can bind in the markup to the value: Checked='<%# Eval("C") %>'

<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Q" HeaderText="Q" />
        <asp:TemplateField HeaderText="C">
            <ItemTemplate>
                <asp:CheckBox ID="CCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="CCheckbox_CheckedChanged" Checked='<%# Eval("C") %>' />
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField HeaderText="R">
            <ItemTemplate>
                <asp:CheckBox ID="RCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="RCheckbox_CheckedChanged" Checked='<%# Eval("E") %>' />
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField HeaderText="E">
            <ItemTemplate>
                <asp:CheckBox ID="ECheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="ECheckbox_CheckedChanged" Checked='<%# Eval("R") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Or you can use rowdatabound event: OnRowDataBound="SearchGrid_RowDataBound"

MarkUP Change:

<asp:GridView ID="SearchGrid" runat="server" GridLines="Both" AutoGenerateColumns="false" OnRowDataBound="SearchGrid_RowDataBound">

Codebehind: Note i used a list object instead of a datatable.

public class TestObject
{
    public string Q { get; set; }
    public bool C { get; set; }
    public bool R { get; set; }
    public bool E { get; set; }
}
public partial class Default : System.Web.UI.Page
{
    public List<TestObject> Values { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //NEED SOME DATA TO TEST THE RESULT
            Values = new List<TestObject>();

            Values.Add(new TestObject() { Q = "test 1", C = true, E = true, R = true });
            Values.Add(new TestObject() { Q = "test 1", C = true, E = false, R = true });
            Values.Add(new TestObject() { Q = "test 1", C = true, E = true, R = false });
            Values.Add(new TestObject() { Q = "test 1", C = false, E = true, R = true });
            //BIND TO THE GRID
            SearchGrid.DataSource = Values;
            SearchGrid.DataBind();
        }
    }

    //FIRES FOR EVERY ROW IN THE GRID
    protected void SearchGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //WE ONLY CARE ABOUT THE DATAROW NOT HEADER ETC
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;
        //GET THE BOUND INDIVIDUAL ITEM
        TestObject obj = (TestObject)e.Row.DataItem; //IN YOUR CASE THIS WOULD BE: DataRow row = (DataRow)e.Row.DataItem

        //FIND ALL THE CHECKBOXES
        CheckBox cchk = e.Row.FindControl("CCheckBox") as CheckBox;
        CheckBox echk = e.Row.FindControl("ECheckBox") as CheckBox;
        CheckBox rchk = e.Row.FindControl("RCheckBox") as CheckBox;

        //CHECK IT OR NOT BASED ON THE DATATITEMS VALUE
        if (cchk != null)
            cchk.Checked = obj.C; //IN YOUR CASE THIS WOULD BE: bool c = (bool)row["C"];

        if (echk != null)
            echk.Checked = obj.E;

        if (rchk != null)
            rchk.Checked = obj.R;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

hi thanks this worked. but now my problem is after adding another row, the previous row Checkboxes gets unchecked, i enabled view state for check boxes but still the pervious rows checkboxes gets unchecked. any solution for this problem?
That is somewhat of a difficult question to answer. Are you rebinding the grid after you add the row (calling .databind())? Are you adding the row to the datatable or directly to the grid? Are you using the same method to set e,c, and r to their respectable values on every call? I would recommend you debug the method you are using to create the column and make sure they have the right values.
i am actually creating a data row every time, then adding it to a table and then binding the grid control to the data table
It looks like on postback your data is lost and replaced with the data in the datatable and the checkbox change was never committed to the datatable?
yeah but i cant put a check box in a data table right? how can i refer a check box values in a data table? can we put controls in a data table?
|
0

You can try to check out the functionality of GridView.RowDataBound. Use this event to capture the row when it has data bound to it and then you should be able to access the CheckBox item and assign its .Checked value.

Link: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Comments

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.