0

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();
}
4
  • You want disable only adding new rows? Commented Oct 13, 2016 at 6:34
  • @mww: yes, I want to disable only that Commented Oct 13, 2016 at 6:36
  • Please show also how you create checkboxlist Commented Oct 13, 2016 at 6:45
  • @mww: see the updated question Commented Oct 13, 2016 at 6:46

2 Answers 2

2

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;
            }
        }

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

2 Comments

did you read my question, I just want it on Registration / Conveyance Deed condition which is not getting fired. Only the Agreement condition is fired..
when I add the else part with true it doesnt works, but when I remove the else part it works well..!!
1

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";
        }
    }

1 Comment

liked the logic, let me try and check..!

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.