HEEN
HEEN

Reputation: 4721

Disable gridview on checkboxlist selected value

I have a Checkboxlist in which Items are binded from database.

Now what I want is, whenever user checks Registration / Conveyance Deed value from the list, Then the gridview should get disabled.

I tried with below code.

protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlStatus.SelectedValue == "30" && strMode == "M")
    {
        GridExpInfo.AllowAddingRecords = false;
    }
    else
    {
        GridExpInfo.AllowAddingRecords = true;
    }
}

What happens is, it always shows the selected value of Agreement from the list, which is 20

Below is the screenshot of the list

Img

update

ASPX:-

<asp:CheckBoxList ID="ddlStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged">
                </asp:CheckBoxList>

C#

private void BindStatus()
{
    DataTable dtstatus = new DataTable();
    OracleDataAdapter dastatus = new OracleDataAdapter("SELECT lookup_code agr_type_code, meaning agr_type " +
                                                        "FROM apps.fnd_lookup_values_vl " +
                                                    "WHERE (NVL ('', territory_code) = territory_code OR territory_code IS NULL " +
                                                           ") AND lookup_type = 'XXACL_FARM_AGR_TYPE' " +
                                                       "AND (lookup_type LIKE 'XXACL_FARM_AGR_TYPE') " +
                                                       "AND (view_application_id = 0) " +
                                                       "AND (security_group_id = 0) " +
                                                     "ORDER BY 1", ObjPriCon);
    dastatus.Fill(dtstatus);
    ddlStatus.DataTextField = "agr_type";
    ddlStatus.DataValueField = "agr_type_code";
    ddlStatus.DataSource = dtstatus;
    ddlStatus.DataBind();
}

Upvotes: 0

Views: 101

Answers (2)

M. Wiśnicki
M. Wiśnicki

Reputation: 6203

You can set property AllowUserToAddRows to false inside code as shown below.

this.yourGrid.AllowUserToAddRows = false;
this.BindDataGridView();

And use ForEach loop for checking your condition. Now you check all selected items not only first selected.

 protected void ddlStatus_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (ListItem li in ddlStatus.Items)
        {
            if (li.Value == "30" && strMode == "M")
            {
                GridExpInfo.AllowUserToAddRows  = false;
            }
            else
            {
                GridExpInfo.AllowUserToAddRows = true;
            }
        }

You can also use LINQ for checking it.

   protected void ddlStatus_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (strMode == "M")
        {
            var disable = (from ListItem item in ddlStatus.Items.OfType<ListItem>()
                where item.Selected
                where item.Value == "30"
                select int.Parse(item.Value)).Any();

             if (disable)
            {
                GridExpInfo.AllowUserToAddRows  = false;
            }
            else
            {
                GridExpInfo.AllowUserToAddRows = true;
            }
        }

    }

Upvotes: 2

VDWWD
VDWWD

Reputation: 35514

Try this snippet

    protected void myCheckBoxList_SelectedIndexChanged(object sender, EventArgs e)
    {
        bool enableGrid = true;
        foreach (ListItem listItem in ddlStatus.Items)
        {
            if (listItem.Value == "30" && listItem.Selected == true)
            {
                enableGrid = false;
            }
        }

        if (enableGrid == true)
        {
            //enable grid and/or row inserts here
            Label1.Text = "enabled";
        }
        else
        {
            //disable grid and/or row inserts here
            Label1.Text = "disabled";
        }
    }

Upvotes: 1

Related Questions