0

I have gridview with checkbox in first column. When check box is clicked, I want to disable the row, but keep the checkbox enabled.

This code disables the whole row including the checkbox:

protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
     CheckBox chkRow = (CheckBox)sender;
     GridViewRow row = (GridViewRow)chkRow.NamingContainer;

     if (chkRow.Checked)
     {
         // Enable the row
         row.Enabled = false;
         row.Style["background-color"] = "#F5F5F5";
         chkRow.Enabled = true;
     }
     else
     {
         // ....
     }
}

1 Answer 1

0

Ok, so say this markup:

<asp:GridView ID="GVHotels" runat="server" AutoGenerateColumns="False"
    DataKeyNames="ID" 
    CssClass="table table-hover" 
    width="50%">
    <Columns>
        <asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate > 
                <asp:CheckBox ID="chkRow" runat="server"
                    OnCheckedChanged="chkRow_CheckedChanged"
                    AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:TextBox ID="txtDesc" runat="server"
                    TextMode="MultiLine" Rows="4" Width="100%"
                    Text='<%# Eval("Description") %>'>
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And code behind to load, and deal with check box:

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
            LoadGrid();

    }

    void LoadGrid()
    {
        string strSQL =
            @"SELECT * FROM tblHotelsA
            WHERE Description is not null
            AND Active = 1
            ORDER BY HotelName";

        GVHotels.DataSource = General.MyRst(strSQL);
        GVHotels.DataBind();
    }

    protected void chkRow_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkRow = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chkRow.NamingContainer;

        if (chkRow.Checked)
            row.Style["background-color"] = "skyblue";  // color row for checked box
        else
            row.Style["background-color"] = "white";  // color row for un-checked box

        // now enable-disable all controls in row EXCEPT for first cell (checkbox)
        for (int i=1;i < row.Cells.Count;i++)
            row.Cells[i].Enabled = !chkRow.Checked;

    }

So, in place of "whole" row disable, we simply disable (or enable) all the cells in that given row, skipping the first cell that contains the check box control.

The result is thus this:

enter image description here

As above shows, when I check box a row, then I am unable to edit/change the given text box. Hence, any column, including templated columns will be disabled with above example.

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

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.