-1

我在 Gridview 中有一个下拉列表,我必须显示与每个 id 关联的记录。ID 包含超过 10 条记录,所以我该如何显示它们?

 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();

            }
        }
4

2 回答 2

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

我这样做是为了将数据库中的电话联系人放入下拉列表中,您可以根据需要更改存储过程和值。

希望这会有所帮助:D

于 2012-10-24T06:15:34.150 回答
0

我们刚刚在我工作的地方遇到了这个问题。我们解决这个问题的方法是首先获取 DropDownLists UniqueID。这基本上是一个客户 ID。该 ID 内部是对从中选择它的 GridView 行的引用。唯一的问题是它似乎在行数中增加了 2。因此,如果您选择第 1 行的 DropdownList,唯一 ID 将为您带来对第 3 行的引用。所以:

获取唯一 ID > 拆分它,但是您需要获取行 > 使用行号来获取您需要的值。

于 2012-10-24T19:49:46.163 回答