gridview如何在gridview asp.net中垂直显示特定列中的数据c#

时间:2011-07-07 09:17:23

标签: gridview

我想垂直显示gridview列中的数据

例如

actual

  1    abc      abc,xyx,mno,qrst

expected 

  1    abc        abc
                  xyz
                  mno
                  qrst

先谢谢,分享你的想法

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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/>";
            }
        }
    }

答案 2 :(得分: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));
        }
    }