Showing number in 2 decimal places in GridView - asp.net

I have one GridView in a .aspx page, and am showing dynamic data in this grid, setting AutoGenerateColumns="True".
Depending upon which of the options the user selects in a combo box, I am binding different DataTables to the GridView. For example, if the user selects Persons then I am fetching the Persons DataTable, and if the user selects Products then I am fetching Products DataTable.
How can I show a float or double number in 2 decimal places in GridView?

The bound column should have a DataFormatString column. You could do something like:
DataFormatString="{0:0.00}"
Numeric Custom Format Strings
UPDATE
In the case of AutoGenerateColumns="true"... I'd have to know more specifics about what you're binding, but here are some avenues to explore:
I'm not sure if GridView will
respect the DataFormatAttribute in
Data Annotations. If you are binding
an object, and GridView respects
that attribute, that might be one
route to go.
Wire the RowDataBound event and
inspect each column for potential
decimal values, and format that way.

you can write BoundField in GridView:
<asp:BoundField DataField="amount" DataFormatString="{0:n}" />
you can also write TemplateField in GridView
<asp:TemplateField>
<ItemTemplate>
<%#Eval("amount","{0:n}")%>
</ItemTemplate>
</asp:TemplateField>

You can do DataFormatString="{0:n2}"in your boundfield

This works on a template column, say if you want a decimal out to two places for a ratio (like 1:3)
<%# Eval("somedatacolumn", "1:{0:.##}").ToString() %>

If you use DataFormatString and it does not seem to be doing the trick, add HtmlEncode = "false", for example:
<asp:BoundField DataField="DateScheduled" HeaderText="Date Created" DataFormatString="{0:D}" HtmlEncode="false"/> // date format
<asp:BoundField DataField="Amount" HeaderText="Pay This Amount" DataFormatString="{0:F}" HtmlEncode="false"/> // number format

There are two easy ways to format things in a GridView. The first is given in a previous answer - use the DataFormatString. The second, which sounds like it applies to your situation, where you are dynamically loading the grid, is to change the data going into the grid.
So, rather than returning a number and trying to format it, return a formatted number and let the GridView display it.

<asp:TemplateField HeaderText="Prev Salary" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblGrdSalary" runat="server" Text='<%#Bind("Salary", "{0:n}") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>

Related

Interacting with rad grid? How to declare the custom columns?

In web application[asp.net], i am using telerik grid control, i am bind the data to grid when it is autocompletecolumns=true, now i want to bind the data autogeneratecolumns=false how can i place controls like in asp.net grid we use
<columns><asp:Templatefileds><ItemTemplate><asp:label id ="lblid" runat="Server"
Text='<%# Eval("Empid") #> /></ItemTemplage></asp:templae>
Radgrid also have template column like ASP gridview, you can use template columns as:
<MasterTableView ShowFooter="False">
<Columns>
<telerik:GridTemplateColumn UniqueName="TemplateColumn" SortExpression="CompanyName">
<ItemTemplate>
// Your Logic Here
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
Hope this helps.
When the columns are not autocompleted, you must specify them as tags inside your Columns tag. You must specify the attributes for the tags which denote your columns and in the NeedDatasource event you fill your datasource.

How to display a large amount of data in gridview

I am trying to show a large amount of data in gridview but the problem is that everytime data increase the gridview row size increase automatically.
is there any possible way that the data which is stored in my MS access Database display in multi line instead of one single long line.
If you are populating the GridView using AutoGenerate="true" make if AutoGenerate="false"
Then use asp:TemplateField to populate the GridView.
Now give an ItemStyle-Width and ItemStyle-Wrap.
<asp:TemplateField ItemStyle-Width="50px" ItemStyle-Wrap="true">
<ItemTemplate>
<asp:Label ID="ShipNameLabel" runat="server" Text='<%# Eval("ShipName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The telerik gridview control supports multi line rows http://www.telerik.com/products/aspnet-ajax/grid.aspx.
However, this does cost. You may try displaying only a few columns, then the user has to click on the row to see the other information in a form view.
Don't understand your question fully but it sounds as a Repeater might work better for you where you have more control of the layout of the rendering.
If it is the amount of rows that are the problem I do recommend to introduce paging to limit amount of row displayed at once.
Update:
1) Set up a CssClass for the GridView itself and include the table-layout:fixed style. This tells the browser that you're going to specify the width of each cell. You may also want to include the overall width of the grid here as I mention in (3).
2) The first row of the table sets the width for each cell, and that's usually the HEADER row, not the item row, so use either HeaderStyle-CssClass or HeaderStyle-Width to set the width of the cell.
3) Make certain the table itself is wide enough to hold all of the cells. I added up all of my cell widths and used that to set the width via the CssClass attribute on the GridView.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns='false'>
<Columns>
<asp:BoundField DataField='Name' />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat='server' ReadOnly="true" BorderStyle="None"
TextMode="MultiLine" Text='<%# Bind("Description") %>'
>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You may have to add some styling to gridview via .skin or css

Adding check box to grid view for each row

I am filling a GridView with the data table but on each row I want a check box how can I achieve this?
<Columns>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="false" />
</ItemTemplate>
</asp:TemplateField>
Use this, you will find what you want!
You could use a < asp:TemplateColumn > or, depending on your desired functionality, opt for a repeater control instead.
Right click the grid view and add a template column. Inside that column, you are able to put whatever you want - including a checkbox.

gridview databinder.eval question

We've got a number of gridviews that we're populating with data from an ADABAS datasource via some middleware that, unfortunately, gives us back lots of arrays of stringBuilder that we turn into a datatable, set as the datasource for a gridview, then bind.
It's convoluted, but I'm in a situation where I need to be able to update the gridview (say, drop a row), without going back to the mainframe, deleting the row, and regenerating the datatable, as I would if we had an Oracle datasource.
So, I'm trying to hack something together where on serverside I extract the data from the gridview, build a datatable, delete the given row, then use that datatable as the datasource for the gridview, and rebind.
I can do it on a case by case basis, so long as I know the layout of the gridview. But we're going to be doing this over and over on this current project, I'm trying to figure out a way to abstract it so that I can use it for any gridview I throw at it.
The problem I'm getting is that most of the cols in the gridview are templatefields with asp:labels, where the data is put in with the databinder.eval thing:
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
So, when building my 'fake' datasource, I need to be able to extract from somewhere in the gridview those column names.
Is that information available in the gridview?
Yikes. You are going to need to store the fake datasource column names within the GridView along with the column values. One suggestion would be to add a HiddenField along with each fake datasource field value. In the example below, display the code (value) in the Label and the column (name) in which code maps in the HiddenField:
<asp:TemplateField HeaderText="Code"
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label>
<asp:HiddenField ID="hfCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "codefield")%>'></asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>
It's ugly, but the column names will always be available to you -- no matter what they might be. Best of luck.

Using GridView HyperLinkField to set a CategoryID

I am using asp.net and I'm working with a gridview. I have one of the fields in my GridView set up as a HyperLinkField that represents the name of the category. I pull the name and Id from a database and I can never be sure what they are because they are added separately. I want to be able to pass a changing CategoryId to another page through the query string. How can I do this with a GridView?
In your Hyperlink field, set the DataNavigateUrlFields to your ID column and set the DataNavigateUrlFormatString to the Url to navigate to (where {0} will be replaced by ID)
<asp:HyperLinkField
DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="Target.aspx?ID={0}"
DataTextField="Name"
Target="_blank">
</asp:HyperLinkField>
You can do it by using a <asp:TemplateField /> as one of your columns instead of a <asp:BoundField />
Code example:
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Container.DataItem("name")
</ItemTemplate>
</asp:TemplateField>
There might be a cleaner way of doing this rather than using the anchor element, but you should get the idea.

Resources