2011-09-07 61 views

答えて

14

テンプレート列を使用している場合は、データバインディング式を使用してマークアップからドロップダウンをバインドできます。たとえば、

<asp:TemplateField HeaderText="XYZ"> 
    <ItemTemplate> 
    <asp:DropDownList runat="server" ID="MyDD" DataSourceId="MyDataSource" /> 
    </ItemTemplate> 
</asp:TemplateField> 

上記は、ドロップダウンデータが一定の行にまたがっていることを前提としています。それが変化している場合、あなたはGetDropDownData、与えられた行のデータ(データテーブル、リスト、配列)を返すであろうコードビハインドで保護された方法であろうよう

<asp:DropDownList runat="server" DataSource='<%# GetDropDownData(Container) %>' DataTextField="Text" DataValueField="Value" /> 

として、データバインディング式を使用することができます。

コードビハインドでGridView.RowDataBoundイベント(またはRowCreatedイベント)を使用してドロップダウンを埋め込むことができます。例えば、

protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 

    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Find the drop-down (say in 3rd column) 
     var dd = e.Row.Cells[2].Controls[0] as DropDownList; 
     if (null != dd) { 
     // bind it 
     } 

     /* 
     // In case of template fields, use FindControl 
     dd = e.Row.Cells[2].FindControl("MyDD") as DropDownList; 
     */ 
    } 

    } 
+0

は何でしょうか? 1つを他のものよりも優先させる理由はありますか? – Tim

+0

@Tim、RowCreatedも動作します。しかし、初めてグリッドをバインドする場合(ポストバックではなく)、 'RowCreated'はすべてのポストバックで確実に起動しますが、' RowDataBound'では発生しません(私は100%確信していません)。このような場合、ビューステートを使用してドロップダウンを埋め込むことができます。個人的には、マークアップルートが好きです。 – VinayC

+0

これは、コード内にSQLインスタンスが宣言されていることを前提としています。コード内にグリッドビューを既にバインドしている場合は、別のソリューションが必要です。マークアップを使用してDataSourceId = "MyDataSource" –

15

は、提案された方法に加えて、あなたもこの方法で、あなたのマークアップ内のあなたのコントロールをバインドすることがあります。

<asp:GridView ID="MyGrid" runat="server" DataSourceID="MyDataSource1"> 
    <Columns> 
     <asp:TemplateField> 
      <EditItemTemplate> 
       <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind ("CustomerId") %>' DataSourceID="CustomersDataSource" DataTextField="CustomerName" DataValueField="CustomerId" > 
       </asp:DropDownList> 
      </EditItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 
+1

+1を使用することはできません。 – Tim

+1

@Tim :ありがとう:) –

+1

@Efranこれは私の問題のために働いた。ありがとう。 – htm11h

-1
protected void gvSalesAppData_RowDataBound(Object sender, GridViewRowEventArgs e) 

    { 

     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

      DropDownList ddlCurrentPhase = (DropDownList)e.Row.FindControl("ddlCurrentPhase"); 
      DropDownList ddlProductFamily = (DropDownList)e.Row.FindControl("ddlProductFamily"); 
      DropDownList ddlProductGroup = (DropDownList)e.Row.FindControl("ddlProductGroup"); 
      DropDownList ddlETProgramManager = (DropDownList)e.Row.FindControl("ddlETProgramManager"); 
      DropDownList ddlPLMForTheProduct = (DropDownList)e.Row.FindControl("ddlPLMForTheProduct"); 

      TrackingToolObj.BindCurrentPhases(ddlCurrentPhase); 
      TrackingToolObj.BindCurrentPhases(ddlProductFamily); 
      TrackingToolObj.BindProductGroups(ddlProductGroup); 
      TrackingToolObj.GetEmployeesBasedOnRoleTypeId(ddlETProgramManager, (int)OSAEnums.RoleTypes.ProgramManager, false); 
      TrackingToolObj.GetEmployeesBasedOnRoleTypeId(ddlPLMForTheProduct, (int)OSAEnums.RoleTypes.PLM, false); 


     } 

    } 
-2

は、GridViewの

バインディングの下にありますGridViewコントロールをデータでバインドするコード。ここで

C#

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
    this.BindData(); 
    } 
} 

private void BindData() 
{ 
    string query = "SELECT top 10 * FROM Customers";  
    SqlCommand cmd = new SqlCommand(query);  
    gvCustomers.DataSource = GetData(cmd);  
    gvCustomers.DataBind(); 
} 

private DataTable GetData(SqlCommand cmd) 
{  
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;  
    using (SqlConnection con = new SqlConnection(strConnString)) 
    {   
     using (SqlDataAdapter sda = new SqlDataAdapter())  
     {    
      cmd.Connection = con;    
      sda.SelectCommand = cmd;    
      using (DataTable dt = new DataTable()) 
      {     
       sda.Fill(dt);     
       return dt; 
      }  
     } 
    } 
} 
+0

あなたの答えを編集し、コードを読みやすくするためにフォーマットしてください。 – kleopatra

+2

正直言って、それはやっかいな方法です。グリッドビューに500行がある場合は、データベースを500回クエリしますか? これを行う必要があるときは、ドロップダウンリストのアイテムのデータでデータセットを塗りつぶし、上記のようにRowDataBoundイベントでデータセットをドロップダウンリストにバインドします。 –

1

あなたのGridViewは

<asp:GridView ID="grvExcelData" runat="server" onrowdatabound="GridView2_RowDataBound"> 
    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" /> 
     <Columns> 
      <asp:TemplateField> 
       <ItemTemplate> 
       <asp:DropDownList ID="DrdDatabase" Width="100px" runat="server"> 
       </asp:DropDownList> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

で、GridViewのためのあなたのRowDataBoundイベントがRowCreatedイベントについて

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     string cities = "maxico,chennai,newdelhi,hongkong"; 
     string [] arr = cities.Split(','); 
    // Instead of string array it could be your data retrieved from database. 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DropDownList ddl = (DropDownList)e.Row.FindControl("DrdDatabase"); 
      foreach (string colName in arr) 
       ddl.Items.Add(new ListItem(colName)); 
     } 
    } 
+0

これはデータベースにバインドされているのではなく、都市の配列にアタッチされているように見えます。ユーザーがドロップダウンを変更した場合、どのようにデータベースが更新されますか? –

関連する問題