2011-07-07 63 views

回答

0

試試看看這個代碼。

   <asp:TemplateField HeaderText="collection" > 
       <ItemTemplate> 
        <asp:Label ID="lblcollection" runat="server" Text='<%# Lookupcollection(DataBinder.Eval(Container.DataItem, "collection")) %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 

protected string Lookupcollection(object idObj) 
{ 
    string result; 
    result = ""; 
    if (string.IsNullOrEmpty(idObj.ToString())) 
     return null; 
    string strCollection = idObj.ToString(); 
    string[] words = strCollection.Split(','); 
    foreach (string word in words) 
    { 
     result = result + word + "<br>"; 
    } 
    return result; 
} 
0

試試這個:

<asp:GridView ID="GridView1" runat="server" 
     onrowdatabound="GridView1_RowDataBound"> 
    </asp:GridView> 


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      string[] data = e.Row.Cells[1].Text.Split(','); 
      e.Row.Cells[1].Text = ""; 
      foreach(string str in data) 
      { 
       e.Row.Cells[1].Text += str+"<br/>"; 
      } 
     } 
    } 
0

你需要做的是 你的GridView:

 <asp:GridView ID="grvExcelData" runat="server" AutoGenerateColumns="false" 
     onrowdatabound="GridView2_RowDataBound"> 
     <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" /> 
     <Columns> 
      <asp:TemplateField ItemStyle-Width = "150px" HeaderText = "UserId"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtUserId" runat="server" 
           CssClass="css2" Text='<%# Eval("UserId")%>'></asp:TextBox> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:TextBox ID="txtUserId" runat="server" 
           CssClass="css2" Text='<%# Eval("UserId")%>'> 
        </asp:TextBox> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Name"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtName" runat="server" 
           CssClass="css2" Text='<%# Eval("Name")%>'></asp:TextBox> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:TextBox ID="txtName" runat="server" 
           CssClass="css2" Text='<%# Eval("Name")%>'> 
        </asp:TextBox> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Select"> 
       <ItemTemplate> 
        <asp:DropDownList ID="drpDetails" Width="100px" runat="server"> 
        </asp:DropDownList> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:DropDownList ID="drpDetails" Width="100px" runat="server"> 
        </asp:DropDownList> 
       </EditItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

和你的代碼你DataRowBound事件背後應該是:

protected void grvExcelData_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     int id = 1; 
     string name = "abc"; 
     string details = "abc,xyx,mno,qrst"; 
     string[] arr = details.Split(','); 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      TextBox UserName = (TextBox)e.Row.FindControl("txtUserId"); 
      TextBox Name = (TextBox)e.Row.FindControl("txtName"); 

      UserName.Text = id.ToString(); 
      Name.Text = name; 
      DropDownList ddl = (DropDownList)e.Row.FindControl("drpDetails"); 
      foreach (string colName in arr) 
       ddl.Items.Add(new ListItem(colName)); 
     } 
    } 
相關問題