Reputation: 62
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
Upvotes: 0
Views: 918
Reputation: 62
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:
onrowcreated="ArrayDataView_RowCreated"
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:
protected void ArrayDataView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlStrings");
ddl.DataSource = (e.Row.DataItem as MyData).Amalgamation.Split(';');
}
}
}
I dropped the eval function in the aspx page and the item template in the gridview was just reduced to housing the drop down:
<ItemTemplate>
<asp:DropDownList ID="ddlStrings" AutoPostBack="false" runat="server"></asp:DropDownList></ItemTemplate>
Thanks to all who helped!
Upvotes: 0
Reputation: 549
I think you could do that, but you wouldn't have to bind to a dropdown if it is just for display. You could just add the markup to make a standard html dropdown list.
If you want to get fancy, how about a function that returns some html with a which shows the first few items, a "..." if there are more, and some script to expand it to show the whole list when you mouseover the , or something like that?
Upvotes: 0
Reputation: 13965
I am assuming that you only want to display this data. If you want the user to be able to edit the data but to be able to reassemble it into a delimited string, that's a much more complex problem -- so much so that you might want to reconsider how you are storing the data in the database.
The easiest way to turn a delimited string into an array of strings is to use the String.Split
method:
string s = "111;333;555;";
string[] sParts = s.Split(";");
You might have to do this in code-behind, though, rather than in a binding expression, because you not only need to split the string, you probably want to bind it to something. There are a number of options: you might like the BulletedList
web control.
Upvotes: 1
Reputation: 3453
Binding to a dropdown seems to be a good idea...if at all you need to edit this in <EditItemTemplate>
, for displaying purpose this is good enough...
Upvotes: 0