locked
MultiSelect List Box RRS feed

  • Question

  • User-1499457942 posted

    hi

       I am using MultiSelect List Item in GridView & saving the Data with comma separated values in a field. How to show the selected List Items when i fill the Grid.

    Thanks

    Monday, August 20, 2018 10:48 AM

All replies

  • User475983607 posted

    I am using MultiSelect List Item in GridView & saving the Data with comma separated values in a field. How to show the selected List Items when i fill the Grid.

    You should use standard schema design practices and use a JOIN, one-to-many or many-to-many, rather than comma separated list in a table column.  Otherwise, you'll need to create the join in code which is much harder and the problem you are facing now.

    Monday, August 20, 2018 11:15 AM
  • User-369506445 posted

    hi

    in below sample shows how to get <g class="gr_ gr_39 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="39" data-gr-id="39">listbox</g>  items and put in a string value

    string str;
            protected void btn_Click(object sender, EventArgs e)
            {
    
                foreach (GridViewRow row in Review_grid.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        var  listBoxes = row.FindControl("lstBox") as ListBox;
                        foreach (ListItem li in listBoxes.Items)
                        {
                            if (li.Selected)
                            {
                                str += li.Text + ",";
                            }
                        }
    
                    }
                }
    
            }

    now you can store the str <g class="gr_ gr_148 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="148" data-gr-id="148">varablein</g> your database

    Monday, August 20, 2018 11:17 AM
  • User-1499457942 posted

    Hi Vahid

       Currenty i am doing like this. I want that it should display all Items & highlight those which have been saved in the database for that row.

    if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                    {
                        string strQuery = "Select Distinct(Location) from Location";
                        SqlDataAdapter sda0 = new SqlDataAdapter(strQuery, con);
                        DataTable dt0 = new DataTable();
                        sda0.Fill(dt0);
    
                        ListBox lst = (ListBox)e.Row.FindControl("lstBox");
                        lst.DataSource = dt0;
                        lst.DataTextField = "Location";
                        lst.DataValueField = "Location";
                        lst.DataBind();
                    }
                }

    Thanks

    Monday, August 20, 2018 11:21 AM
  • User-369506445 posted

    suppose your query return a string below like

      string str= "Item1,Item2,Item3,Item4";

    now  you split it first  and check it with each item of <g class="gr_ gr_106 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="106" data-gr-id="106">listbox</g> below like 

    var arr = str.Split(',');
    
                foreach (GridViewRow row in Review_grid.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        var  listBoxes = row.FindControl("lstBox") as ListBox;
                        foreach (ListItem li in listBoxes.Items)
                        {
    
                            if (arr.Any(x => x.Equals(li.Text)))
                            {
                                li.Selected = true;
                            }
                        }
    
                    }
                }
    
    

    Monday, August 20, 2018 11:45 AM
  • User475983607 posted

    As stated above, you should fix the design and use table joins rather than a comma separated string.  The design is much easier.  Otherwise you'll need to fetch the split the string, loop over the the string items, find the matching listbox value or items depending on how you did this, then set the Selected property.

    Monday, August 20, 2018 11:59 AM
  • User-1499457942 posted

    Hi

      I have to write both codes . First mine one then your code

    Thanks

    Monday, August 20, 2018 12:05 PM
  • User-369506445 posted

    did you try my code? did you have problems? or ...

    please put here your feedback

    Monday, August 20, 2018 12:12 PM