Populate dropdownlist inside a gridview

2020-05-09 22: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??

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                con.Open();
                var ddl = (DropDownList)e.Row.FindControl("DropDownList1");
                //int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
                SqlCommand cmd = new SqlCommand("select LastName from Profile_Master",        con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                con.Close();
                ddl.DataSource = ds;
                ddl.DataTextField = "LastName";
                ddl.DataBind();

            }
        }

2条回答
▲ chillily
2楼-- · 2020-05-09 22:36
        FillSelect(myDropDownList, "--select--", "0", true);
        public static void FillSelect(DropDownList DropDown, string SelectItemText, string SelectItemValue, bool includeselectitem)
        {
            List<PhoneContact> 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<PhoneContact> getAll()
        {

        obj_PhoneContactlist = new List<PhoneContact>();
        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

查看更多
Rolldiameter
3楼-- · 2020-05-09 22:57

We just ran into this issue where I work. Our way around this problem was to first get the DropDownLists UniqueID. This is basically a Client ID. Inside of that ID is a reference to the row of the GridView that it was selected from. THE ONLY PROBLEM is that it seems to add 2 to the row count. So if you select Row 1's DropdownList, the Unique ID will bring you a reference to the 3rd row. So:

Get the unique ID > Split it however you need to to get the row > use the row number to get the values you need.

查看更多
登录 后发表回答