我想顯示在gridview的列中的數據垂直怎麼辦的GridView如何dispaly在特定列數據在垂直的GridView asp.net C#
例如
actual
1 abc abc,xyx,mno,qrst
expected
1 abc abc
xyz
mno
qrst
預先感謝PLZ共享烏爾想法
我想顯示在gridview的列中的數據垂直怎麼辦的GridView如何dispaly在特定列數據在垂直的GridView asp.net C#
例如
actual
1 abc abc,xyx,mno,qrst
expected
1 abc abc
xyz
mno
qrst
預先感謝PLZ共享烏爾想法
試試看看這個代碼。
<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;
}
試試這個:
<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/>";
}
}
}
你需要做的是 你的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));
}
}
嗯這可以通過spli完成以編程方式處理數據,但確保數據以逗號分隔 – SMK