2014-09-25 3 views
0

다음은 현재 그리드보기입니다.GridView의 확인란이있는 DropdownList

<asp:GridView ID="grdIndexGroupMap" runat="server" AutoGenerateColumns="False" DataKeyNames="IndexName" 
      OnRowCancelingEdit="grdIndexGroupMap_RowCancelingEdit" OnRowDataBound="grdIndexGroupMap_RowDataBound" 
      OnRowEditing="grdIndexGroupMap_RowEditing" OnRowUpdating="grdIndexGroupMap_RowUpdating" 
      OnRowCommand="grdIndexGroupMap_RowCommand" ShowFooter="True" OnRowDeleting="grdIndexGroupMap_RowDeleting" 
      CellPadding="1" CellSpacing="1" ForeColor="#333333" GridLines="None"> 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
      <Columns> 
       <%--IndexName--%> 
       <asp:TemplateField HeaderText="IndexName" HeaderStyle-HorizontalAlign="Left"> 
        <EditItemTemplate> 
         <asp:DropDownList ID="cmbIndexName" runat="server" DataTextField="LocationName" DataValueField="IndexId"></asp:DropDownList> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Label ID="lblIndexName" runat="server" Text='<%# Eval("IndexName") %>'></asp:Label> 
        </ItemTemplate> 
        <FooterTemplate> 
         <asp:DropDownList ID="cmbNewIndexName" runat="server" DataTextField="IndexName" DataValueField="IndexId"></asp:DropDownList> 
        </FooterTemplate> 
        <HeaderStyle HorizontalAlign="Left"></HeaderStyle> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

DropDownList를 여러 항목을 선택할 수있는 드롭 다운으로 대체하는 방법은 무엇입니까? 드롭 다운 목록의 체크 박스 목록 또는 드롭 다운의 다중 선택 목록 상자. 선택하면 쉼표로 구분 된 값이 표시됩니다.

몇 가지 방법을 시도했지만 작동하지 않습니다. 여기

내 데이터 바인딩 방법입니다 : 내가 ASP.Net을 사용하고

protected void grdIndexGroupMap_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       DropDownList cmbIndexName = (DropDownList)e.Row.FindControl("cmbIndexName"); 
       if (cmbIndexName != null) 
       { 
        cmbIndexName.DataSource = _Indexes; 
        cmbIndexName.DataTextField = "IndexName"; 
        cmbIndexName.DataValueField = "IndexId"; 
        cmbIndexName.DataBind(); 
        cmbIndexName.SelectedValue = grdIndexGroupMap.DataKeys[e.Row.RowIndex].Values[1].ToString(); 
       } 
      } 
      if (e.Row.RowType == DataControlRowType.Footer) 
      { 
       DropDownList cmbNewIndexName = (DropDownList)e.Row.FindControl("cmbNewIndexName"); 
       cmbNewIndexName.DataSource = _Indexes; 
       cmbNewIndexName.DataBind(); 
      } 
     } 

, C#을

+0

을 통해 찾아하시기 바랍니다 특히위한'OnRowDataBound()' – geedubb

+0

추가 OnRowDataBound 방법 뒤에 당신의 코드를 참조하는 것이 유용 할 것이다. – Cannon

답변

0

방법에 대해 :

if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    phListContainer = (PlaceHolder)e.Row.FindControl(phListContainer); 

    if (phListContainer != null) 
    { 
     //Adding a DropDownList 
     var cmbIndexName = new DropDownList(); 
     cmbIndexName.DataSource = _Indexes; 
     cmbIndexName.DataTextField = "IndexName"; 
     cmbIndexName.DataValueField = "IndexId"; 
     cmbIndexName.DataBind(); 
     cmbIndexName.SelectedValue = grdIndexGroupMap.DataKeys[e.Row.RowIndex].Values[1].ToString(); 

     phListContainer.Controls.Add(cmbIndexName); 

     // OR 

     //Adding a CheckBoxList; 
     cmbIndexName = new CheckBoxList(); 
     cmbIndexName.DataSource = _Indexes; 
     cmbIndexName.DataTextField = "IndexName"; 
     cmbIndexName.DataValueField = "IndexId"; 
     cmbIndexName.DataBind(); 

     phListContainer.Controls.Add(cmbIndexName); 
    } 
} 
: 당신의 코드 숨김에서 다음

<asp:TemplateField HeaderText="IndexName" HeaderStyle-HorizontalAlign="Left"> 
    <EditItemTemplate> 
     <asp:PlaceHolder id="phListContainer" runat="server" /> 
    </EditItemTemplate> 

그러면 다음을 얻을 수 있습니다. 전자

var cbList = (CheckBoxList)e.Row.FindControl(phListContainer).Controls[0]; 

에 의해 제어하고 당신이 할 수있는 루프 체크 박스

for(int i = 0; i < cbList.Items.Count; ++i) 
{ 
if(cbList.Items[i].Selected) 
//Do stuff 
} 
관련 문제