Display $ in gridview Price Column - asp.net

<asp:BoundField DataField="ProductPrice" HeaderText="Price" />
How can I display a dollor sign before each record in the above column for every row?

You can format your data value as Currency by using DataFormatString
<asp:BoundField DataField="ProductPrice" HeaderText="Price" DataFormatString="{0:C}" />
if the culture is setted to a country who has a dollar currency, it will work fine.

Use TemplateField instead of BoundField, you can format it like anything you want
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
$<%# Eval("ProductPrice")%>
</ItemTemplate>
</asp:TemplateField>

Related

asp.net (vb) gridview how to calculate two columns and pass the value to a third column?

I'm binding my gridview with DB values. I have three columns which are: Earnings, Discount and Total. The Earning and Discount columns are bind with DB values and the Total column is empty. I want to bind the total column with the sum result of the columns (Earnings + Discount).
how can I bind the Total column with the sum result of (Earning + Discount) columns?
My code
<asp:BoundField DataField="Gross_Period1" HeaderText="Earnings" />
<asp:BoundField DataField="Disc_Period1" HeaderText="Discount" />
<asp:BoundField HeaderText="Total" />
You can as noted, just include the column in your sql.
However, you can do it this way also:
add column like this:
<asp:BoundField DataField="Gross_Period1" HeaderText="Earnings" />
<asp:BoundField DataField="Disc_Period1" HeaderText="Discount" />
<asp:BoundField HeaderText="Total" />
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:label Text='<%#Eval("ID") * 10 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
So using "Template" allows you to drop, have, put, place any control of any type into the grid. (in above, you can thus remove the HeaderText total bound field you have.
And you can thus use Eval as a expression.
So, in your case, you could have Text above with this:
Text='<%#Eval("Earnings") + Eval("Discount") %>'
So that will give the addition of the two columns.

GridView Column with long text

I have a GridView with a number of columns and it is all populating how I want it however one of the columns can have quite a bit of text in it. To try and present it nicely I'd prefer to be able to show that column in the row under it, so spanned across all columns:
As a simplified example currently I have:
Name DOB Sex Comments Title
Ian 15/04/2015 M This is MR
the text
that keeps
wrapping
I'd prefer it to be something like
Name DOB Sex Comments Title
Ian 15/04/2015 M This is MR
Comments: This is the text that keeps wrapping
Obviously this is a very simplified example but hopefully it explains what I'm trying to do. The current GridView code I have is below, I'm wanting to move intScore to the next row.
<asp:GridView ID="gvwClinical" runat="server" DataKeyNames="intClinicalAssessID" OnRowDataBound="gvwGrids_RowBound" OnRowDeleting="gvwGrids_Delete" OnSelectedIndexChanging="gvwClinical_SelectedIndexChanged" OnSorting="gvwSort" OnPageIndexChanging="gvwPage">
<Columns>
<asp:BoundField Visible="False" DataField="intClinicalAssessID"></asp:BoundField>
<asp:ButtonField Text="View" CommandName="Select" ButtonType="image"></asp:ButtonField>
<asp:ButtonField Text="Delete" CommandName="Delete" ButtonType="image"></asp:ButtonField>
<asp:BoundField DataField="dteAssessmentDate" HeaderText="Assess. Date" SortExpression="dteAssessmentDate"></asp:BoundField>
<asp:BoundField DataField="intScores" HeaderText="Scores" SortExpression="intScores" HtmlEncode="false"></asp:BoundField>
<asp:BoundField DataField="intHeight" HeaderText="Height" SortExpression="intHeight"></asp:BoundField>
<asp:BoundField DataField="intWeight" HeaderText="Weight" SortExpression="intWeight"></asp:BoundField>
<asp:BoundField DataField="strWellBeing" HeaderText="Gen Well Being" SortExpression="strWellBeing"></asp:BoundField>
<asp:BoundField DataField="strAbdominalPain" HeaderText="Abdo Pain" SortExpression="strAbdominalPain"></asp:BoundField>
<asp:BoundField DataField="strAbdominalMass" HeaderText="Abdo Mass" SortExpression="strAbdominalMass"></asp:BoundField>
<asp:BoundField DataField="strBowelFreqDay" HeaderText="Bowel Freq (day)" SortExpression="strBowelFreqDay"></asp:BoundField>
<asp:BoundField DataField="strBowelFreqNight" HeaderText="Bowel Freq (night)" SortExpression="strBowelFreqNight"></asp:BoundField>
<asp:BoundField DataField="strStoolUrgency" HeaderText="Defecation Urgency" SortExpression="strStoolUrgency"></asp:BoundField>
<asp:BoundField DataField="intLiquidStoolCount" HeaderText="Lq Stools 24 Hrs" SortExpression="intLiquidStoolCount"></asp:BoundField>
<asp:BoundField DataField="strRectalBleeding" HeaderText="Rectal Bleeding" SortExpression="strRectalBleeding"></asp:BoundField>
<asp:BoundField DataField="strEndoscopyFindings" HeaderText="Endo Findings" SortExpression="strEndoscopyFindings"></asp:BoundField>
<asp:BoundField DataField="strGlobalAssess" HeaderText="Gbl Assessment" SortExpression="strGlobalAssess"></asp:BoundField>
<asp:BoundField DataField="intActiveFistula" HeaderText="Active Fistula" SortExpression="intActiveFistula"></asp:BoundField>
<asp:BoundField DataField="intTemperature" HeaderText="Temp." SortExpression="intTemperature"></asp:BoundField>
<asp:BoundField DataField="intPulse" HeaderText="Pulse" SortExpression="intPulse"></asp:BoundField>
<asp:BoundField DataField="intHB" HeaderText="Hb" SortExpression="intHB"></asp:BoundField>
<asp:BoundField DataField="intHCT" HeaderText="Hct" SortExpression="intHCT"></asp:BoundField>
<asp:BoundField DataField="intCRP" HeaderText="CRP" SortExpression="intCRP"></asp:BoundField>
<asp:BoundField DataField="strComplications" HeaderText="Complications" SortExpression="strComplications"></asp:BoundField>
</Columns>
</asp:GridView>
You could use a TemplateField for this. This will enable you to put the longer text underneath the rest of the fields since you can design the layout per column, or row if you use only one TemplateField. you can Bind and Eval as many DataFields and html in one TemplateField as you desire if your DataSource provides them:
<Columns>
<asp:TemplateField HeaderText="Header1" SortExpression="fieldname1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("fieldname1") %>'></asp:Label>
<hr>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("fieldname2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Please note tough that this will come with side effects. You can only add a Column Header for each TemplateField you add, so ordering your data for each DataField can become tricky or pretty much impossible with the default setup from a GridView when using these fields. Since TemplateFields behave like normal BoundFields it is possible to combine BoundFields and TemplateFields in a row, but you cannot let a TemplateField show up underneath the other Columns.
As far as i know it's impossible to use BoundFields in a TemplateField, and if that's really what you are searching for maybe looking for a Pivot grid would be a better idea. But if the highly configurability and footprint of a Pivot grid suits your solution is another question.
Hope this helps in finding an awnser to your challenge. Good Luck!

Format error with a date in a asp.net bounded field?

I have a date in a bounded field as shown below:
<asp:BoundField DataField="closeDate" HeaderText="End Date" SortExpression="closeDate" htmlencode="false" dataformatstring="{0:d}" >
<ItemStyle CssClass="georgeTest"></ItemStyle>
<HeaderStyle CssClass="georgeTest"> </HeaderStyle>
</asp:BoundField>
I'm trying to format it in this manner: "12/18/2012" with the dataformatstring attribute using both the format shown above and dataformatstring="{0:dd/MM/yyyy}}" they both give me the same value '1/1/0001' all the time and I can't figure out why this happens.
I am working in MS Visual Studio 2010 and ASP.NET 4.0 .
The field is of the DateTime type.
For reference going forward I've posted the link content from my comment as the answer:
How To Set a Date Format In GridView Using ASP.NET 2.0 (Using HtmlEncode Property)
Posted by Peter Kellner in .Net 2.0, ASP.NET 2.0
(AKA, the DataFormatString="{0:M-dd-yyyy}" Problem)
A very common desire is to set a column of a GridView to display just the month, day and year of a DateTime type. The problem is the by default, the HtmlEncode property of the BoundField attribute (
<columns>
<asp:BoundField headertext="CreationDate" dataformatstring="{0:M-dd-yyyy}"
datafield="CreationDate" />
</columns>
You have two choices to make this work as you would expect. The first choice is to simply set HtmlEncode to false as follows:
<asp:GridView id="GridView1" runat="server" >
<columns>
<asp:BoundField headertext="CreationDate" dataformatstring="{0:M-dd-yyyy}"
datafield="CreationDate" HtmlEncode="false" />
</columns>
The second choice is to make the column a template and simply set the format string directly in the Label or Text Fields as follows.
<asp:GridView id="GridView3" runat="server" >
<columns>
<asp:TemplateField headertext="CreationDate" >
<edititemtemplate>
<asp:Label id="Label1" runat="server" Label.Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp:Label>
</edititemtemplate>
<itemtemplate>
<asp:Label id="Label1" runat="server" Label.Text='<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>;
</asp:Label>
</itemtemplate>
</asp:TemplateField>
</columns>
</asp>

Gridview BoundField Remove Time from DateTime In Edit/Update Mode

I have the following GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="EVENT,TERM_CODE" DataSourceID="OracleDataSource" ShowHeaderWhenEmpty="True"
EnableViewState="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="EVENT" HeaderText="Event" ReadOnly="True"
SortExpression="EVENT" />
<asp:BoundField DataField="TERM_CODE" HeaderText="Term Code" ReadOnly="True"
SortExpression="TERM_CODE" />
<asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date"
SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}" />
</Columns>
</asp:GridView>
The SNAPSHOT_DATA_DATE column is a DateTime field in the database.
I only want to show the date not both the date and time ex. 7/25/2012
So I added DateFormatString="{0:d}" to the BoundField and this works great when viewing the Gridview. However, if I click the edit link to update the date it displays as: 7/25/2012 12:00:00 AM The DateFormatString is ignored in edit mode. Can someone please tell me how to remove the time when in edit / update mode? I don't want users dealing with the time only the date.
Make sure you have set the following properties for formatting to work in Edit Mode in Bound field.
DataFormatString = ({0:d})
ApplyFormatInEditMode = True
In your code if you just modify like below, it should work fine
<asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date" ApplyFormatInEditMode="true" SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}" />
I specifically wanted to format my date as yyyy/MM/dd without the time equivalent to the following:
string dateString = DateTime.Now.ToString("yyyy/MM/dd");
To do this I just added the format on the DataFormatString attribute for the BoundField:
<asp:BoundField DataField="DatePosted" HeaderText="Date" DataFormatString="{0:yyyy/MM/dd}" />

Custom message for BoundField in gridview in HTML markup

I have a BoundField in my gridview. Some of these BoundField are empty if there is no data in the datasource. For such type of fields I want to show N/A, but from the HTML markup only. Is there any way to do so???
This is the code for my grid view which has empty values and for what I want to show N/A
<asp:BoundField DataField="POCreatedDate" HeaderText="Created On" DataFormatString=" {0:MM/dd/yyyy}"
HtmlEncode="false" SortExpression="POCreatedDate"/>
<asp:BoundField DataField="PODueDate" HeaderText="Due Date" DataFormatString="{0:MM/dd/yyyy}"
HtmlEncode="false" SortExpression="PODueDate"/>
NullDisplayText property is all you need :
<asp:BoundField DataField="POCreatedDate" HeaderText="Created On"
DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"
SortExpression="POCreatedDate"
NullDisplayText="N/A" />

Resources