2012-10-24 139 views
-1

我在Gridview中有一個下拉列表,我必須顯示與每個id相關的記錄。並且該ID包含超過10條記錄,所以如何顯示它們?在GridView中填充下拉列表

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

      } 
     } 
+0

你是否嘗試將'ddl.DataValueField'分配給其他一些像ID一樣的列? – Ahmad

+0

沒有我分配這樣的,但有什麼用? –

回答

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

我這樣做讓我phonecontacts這是在數據庫中爲下拉您可以更改存儲過程,並根據您的需要的值。

希望這有助於:d

+0

但是如何獲得與每個ID相關的記錄? –

+0

你必須在存儲過程中更改它,我使用spS_GetMyContacts,因爲我使用該語句來獲取所有聯繫人,而不是您必須根據ID獲取值,只有一條語句更改 – Raghurocks

+0

以獲取根據該ID你甚至可以發送當前的ID作爲參數之一,並根據編號獲取值 – Raghurocks

0

我們只是碰到了這個問題,我的工作。我們解決這個問題的方法是首先獲取DropDownLists UniqueID。這基本上是一個客戶端ID。該ID內部是對從中選擇的GridView行的引用。唯一的問題是它似乎在行數上加2。因此,如果您選擇第1行的DropdownList,則唯一ID會爲您提供對第3行的引用。所以:

獲取唯一ID>將其拆分,但需要獲取該行>使用行號獲取所需的值。