This is a migrated thread and some comments may be shown as answers.

CheckBoxList control issue in a Grid Edit template

5 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
rms
Top achievements
Rank 1
rms asked on 15 May 2012, 08:14 PM
Hi,

My assignment is to give edit functionality in a grid. when the user clicks on edit user will see textboxes and checkboxlists from which he can manipulate data.

In the Form template I have a CheckBoxList.

<EditFormSettings EditFormType = "Template">
<FormTemplate>
<asp:CheckBoxList  ID= "List2"  runat="server" selectedvalue = '<%# EVal("ApprovedValues")%>' datasource= "sqldatasource1"
datatextfield="value" datavaluefield= "value" tabindex="2">
</asp:heckBoxList>

my problem here is the approvedvalues that i get from the grid can look like a,b,c. I want to recognize all these values and select them from the list coming from sqldatasource1. Also if the approvedvalue has data that does not exist in the sqldatasource1 list I am getting an error.

"list2 has a selected value which is invalid because it does not exist in the list of items."

can anyone help?
Thanks

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 May 2012, 07:09 AM
Hello,

<MasterTableView DataKeyNames="ApprovedValues">
<asp:CheckBoxList  ID= "List2"  runat="server"  datasource= "sqldatasource1"
datatextfield="value" datavaluefield= "value" tabindex="2">

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
      if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            //GridEditableItem item = e.Item as GridEditableItem;
            GridEditFormItem item = e.Item as GridEditFormItem;
            CheckBoxList List2 = item.FindControl("List2") as CheckBoxList;
            string strApprovedVlues = Convert.ToString(item.GetDataKeyValue("ApprovedValues"));
            if (!string.IsNullOrEmpty(strApprovedVlues))
            {
                if (strApprovedVlues.Split(',').Length > 0)
                {
                    foreach (string str1 in strApprovedVlues.Split(','))
                    {
                        if (List2.Items.FindByValue(str1) != null)
                        {
                            List2.Items.FindByValue(str1).Selected = true;
                        }
                    }
                }
            }
        }
  
     }


Thanks,
Jayesh Goyani
0
rms
Top achievements
Rank 1
answered on 16 May 2012, 07:37 PM

Thanks Jayesh,

I have tried you solution. most of it works but I am getting a null reference exception near the below line

Dim strApprovedVlues As String = Convert.ToString(Item.GetDataKeyValue("FailReason"))

Item is declare as  

Dim Item As GridDataItem = TryCast(e.Item, GridDataItem)

I have also tried this
Dim strApprovedVlues As String = Item("FailReason").Text

FailReason is a column in my grid.

I thought may be this is because this code is in ItemBound event so i changed it to the RadGrid1_ItemCommand event. in this atleast there is no error but desired functionality is not working.

0
rms
Top achievements
Rank 1
answered on 16 May 2012, 08:40 PM
Also I wanted the mention that I did add

<MasterTableView DataKeyNames="FailReason">
Thanks.
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 17 May 2012, 07:26 AM
Hello,

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = e.Item as GridDataItem;
           string strFailReason = item.GetDataKeyValue("FailReason").ToString();
       }


Thanks,
Jayesh Goyani
0
rms
Top achievements
Rank 1
answered on 17 May 2012, 03:38 PM
But ofcourse. why din't I think of it?
Thanks a lot!
Tags
Grid
Asked by
rms
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
rms
Top achievements
Rank 1
Share this question
or