Populate dropdownlist inside a gridview

后端 未结 2 880
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 06:11

I have a Dropdownlist in a Gridview and i have to show the records associated with every id.And the ID contains more than 10 records so how can i show them??

 pr         


        
2条回答
  •  星月不相逢
    2021-01-29 06:40

            FillSelect(myDropDownList, "--select--", "0", true);
            public static void FillSelect(DropDownList DropDown, string SelectItemText, string SelectItemValue, bool includeselectitem)
            {
                List obj_PhoneContactlist = getAll();
    
                if (obj_PhoneContactlist != null && obj_PhoneContactlist.Count > 0)
                {
                    DropDown.DataTextField = "PhoneContactName";
                    DropDown.DataValueField = "id";
                    DropDown.DataSource = obj_PhoneContactlist.OrderBy(o => o.PhoneContactName);//linq statement
                    DropDown.DataBind();
    
                    if (includeselectitem)
                        DropDown.Items.Insert(0, new ListItem(SelectItemText, SelectItemValue));
                }
            }
            public static List getAll()
            {
    
            obj_PhoneContactlist = new List();
            string QueryString;
            QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["Admin_raghuConnectionString1"].ToString();
    
            obj_SqlConnection = new SqlConnection(QueryString);
    
            obj_SqlCommand = new SqlCommand("spS_GetMyContacts");
            obj_SqlCommand.CommandType = CommandType.StoredProcedure;
            obj_SqlConnection.Open();
            obj_SqlCommand.Connection = obj_SqlConnection;
            SqlDataReader obj_result = null;
            obj_SqlCommand.CommandText = "spS_GetMyContacts";
            obj_result = obj_SqlCommand.ExecuteReader();
    
    
            //here read the individual objects first and append them to the listobject so this we get all the rows in one list object
    
            using (obj_result)
            {
                while (obj_result.Read())
                {
                    obj_PhoneContact = new PhoneContact();
                    obj_PhoneContact.PhoneContactName = Convert.ToString(obj_result["PhoneContactName"]).TrimEnd();
                    obj_PhoneContact.PhoneContactNumber = Convert.ToInt64(obj_result["PhoneContactNumber"]);
                    obj_PhoneContact.id = Convert.ToInt64(obj_result["id"]);
    
                    obj_PhoneContactlist.Add(obj_PhoneContact);
                }
    
            }
    
            return obj_PhoneContactlist;
            }
    

    I have done this to get my phonecontacts which are in the data base into dropdown you can change the stored procedures and the values according to your need.

    Hope this helps:D

提交回复
热议问题