I have data returning to a Gridview in .net from a dynamic database and one of the fields has a long list of semi-colon delimited values. I was able to display this data using line breaks in place of semi-colons however as there is potentially up to 20 values in this field I’m sure there’s a better way of displaying this.
I used the following code to create and amend a template field:
<asp:TemplateField HeaderText="Amalgamation"
SortExpression="Amalgamation">
<ItemTemplate>
<%# Eval("Amalgamation").ToString().Replace(";", "<br />")%>
</ItemTemplate>
</asp:TemplateField>
An example of the data which will populate this field is: 100001;100002;600001;600006.
Update: Ive tried this but had no joy. In the html:
<%#PopulateArray((string)(Eval("Amalgamation")))%>
<asp:DropDownList ID="ddlStrings" AutoPostBack="true" runat="server"></asp:DropDownList>
Then this function in the code behind:
public object PopulateArray(string s)
{
string[] sArray = s.Split(';');
DropDownList ddl = new DropDownList();
ddl = (DropDownList)this.Page.FindControl("ddlStrings");
ddl.DataSource = sArray;
ddl.DataBind();
return sArray;
}
I get a NullReferenceException on the line: ddl.DataSource = sArray
OK I’ve got the bottom of it. I was referencing the ID for the Dropdown instead of referencing each row instance of it. I put the code in the row created event of the gridview using the parameter:
I split the result using the semi-colon delimiter every time a row was created in the table. I also cast each result using the object class (“MyData”) I was using for the database. I then had to filter out the header and footer data rows of the gridview using an “if” statement. Here is the row created code in the code behind:
I dropped the eval function in the aspx page and the item template in the gridview was just reduced to housing the drop down:
Thanks to all who helped!