Reputation: 3440
I can't figure out how to search for this and get exactly what I need. I'm trying to figure out how to change the way a piece of data displays. So, for example, if I retrieve a tinyint
that is either a 1 or 0, and I want to display either "yes" or "no" respectively, how do I go about that? Here is what I have in the aspx:
<ASP:DataGrid id="AdminDataGrid" runat="server"
AutoGenerateColumns="False" AllowSorting="true"
Width="700px" CellPadding="3" EnableViewState="True"
BackColor="#EEEEEE" BorderColor="Black" HeaderStyle-BackColor="Gray"
Font-Name="Verdana" Font-Size="8pt" Font-Names="Verdana"
>
<Columns>
<asp:BoundColumn DataField="Active" HeaderText="Active State" ReadOnly="true" SortExpression="Active" />
</Columns>
</ASP:DataGrid>
To be clear, I just want it to automatically update the value every time it pulls data from the SQL table.
I've been trying a few things, but I'm not sure when and how to make the change. Member objects:
DataTable DataTableAdmin = new DataTable();
DataView DataViewAdmin;
Then in page load:
DataTableAdmin = myDataAccessObject.GetTable(strQuery);
DataViewAdmin = new DataView(DataTableAdmin);
I tried something like:
if (DataTableAdmin.Columns[0] = "0")
{
DataTableAdmin.Columns[0] = "No";
}
else
{
DataTableAdmin.Columns.[0] = "Yes";
}
But I know that's not right because it doesn't know what row to do that on. But I'd like it to be universally applied to all rows anyway. Then I thought I could do something with the DataView
using a filter:
DataViewAdmin.RowFilter("Active = '0'");
But then I'm not sure how I would apply changes to what I get back. Something else I was considering would be to replace the column with a series of checkboxes that would be checked or unchecked depending on what the value is. Then that could be checked or unchecked, and the value could be updated in the database.
Upvotes: 0
Views: 144
Reputation: 3440
C Shaper's answer got me on the right track, but it needed a decent amount of tweaking. First problem was: code blocks are not supported in this context.
Changing the object to a TemplateColumn
fixed that. Then it was failing to parse the value, saying the String was not recognized as a valid Boolean.
After doing further research, I got it working with the following:
<asp:TemplateColumn HeaderText="Active" SortExpression="Active">
<ItemTemplate><%# Convert.ToBoolean(Convert.ToInt32(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>
</asp:TemplateColumn>
Upvotes: 0
Reputation: 5580
This is coming from the Question I posted in the comments. Pretty funny both columns are named Active in the 2 posts.
<asp:TemplateField HeaderText="Active" SortExpression="Active">
<ItemTemplate><%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>
</asp:TemplateField>
Upvotes: 1