So I got a ASPxGridView with few VisibleColumns which shows records of percentage value. I need for some of that columns, ie. VisibleColumns[1] , apply format of theirs value. These records are save in DB like 83.79000. These columns got rounded values in 2 decimal places.
So my first Question: how could i show these values in format like 83.79?
Second Q: when exactly I should do that? If you look at this code snippet, I guess after Databind i got a records in GridView but how modify them and save them to that GridView?
ASPxGridViewMain.DataSource = this.DataSource;
ASPxGridViewMain.DataBind();
Third Q: in purpose of Debugg, how i can see the values of that column?
EDIT: My ASPXGridView like:
<dxwgv:GridViewDataTextColumn FieldName="TRS" VisibleIndex="1" Caption="Hodnota TRS">
</dxwgv:GridViewDataTextColumn>
You can use EditPropertiesBase.DisplayFormatString property to show values in your format. You don't need to modify your records. You can just add this to your aspx code:
<dxwgv:GridViewDataTextColumn FieldName="TRS" VisibleIndex="1" Caption="Hodnota TRS">
<PropertiesTextEdit DisplayFormatString="#.00" />
</dxwgv:GridViewDataTextColumn>
You should use DataFormatString the msdn link is below
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring%28v=vs.110%29.aspx
You should use bound fields to use data format.
<asp:BoundField DataField="amount" HeaderText="amount"
ReadOnly="True" SortExpression="amount" DataFormatString="{0:n3}" />
Edit 1
If you don't want to use BoundField then you should use RowDataBound event of gridview. Get the cell and format as you like.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//get you cell here
//put the number format here.
}
}
You can use bound columns in GridView and specify DataFormatString. Like below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="C1" HeaderText="Display Column Name" DataFormatString="{0:0.00}" />
</Columns>
</asp:GridView>
Related
I want to change the name of one of the column of gridview on runtime.The Gridview has also sorting enabled in it.
the gridview looks like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowCreated="myGrid_Rowcreate"
ShowFooter="false"
AllowSorting="true"
OnSorting="GridView1_Sorting"
EnableViewState="false"
ShowHeaderWhenEmpty="True"
AllowPaging="false">
<AlternatingRowStyle CssClass="altrowstyle" />
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle" />
<RowStyle Wrap="false" />
<HeaderStyle Wrap="false" />
</asp:GridView>
and my onRowCreate function looks like:
protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
if (gvr.RowType == DataControlRowType.Header)
{
gvr.Cells[1].Text = "test1";
}
}
the column name does change to test1 but the sorting feature goes off.what should I do that will change the coulmn name as well as the sorting feature also stays?there is no problem in my sorting.Only when I write the above code,the sorting option for that column goes off.
Please help!
thank you.
The problem is because Sorting sets a LinkButton control in the GridView columns name, and setting the name in the way you do it, disable that control. So, you must set the name of the column with a code like:
((LinkButton)e.Row.Cells[1].Controls[0]).Text = "Test1";
I'm guessing you're trying to use AutoGenerateColumns?
If this is the case why can't you change the column name in the datasource (using "AS" if the datasource is SQL)?
Otherwise, your altering the cell text is short-circuiting the ASP.NET functionality which generates the sorting javascript postback.
Alternatively you would do this if you don't use AutoGenerateColumns:
myGrid.Columns[1].HeaderText = "test1";
I think your problem comes from the postback sent when you order your grid. You don't want to rebind your grid after ordering it.
In your pageLoad you should add :
If(!PostBack)
// the code to bind data to your grid
This way you prevent the grid to be reloaded and the information you set in myGrid_RowDataBound for the name your column should stay the same...
I have a data table which is displayed as a gridview in ASP.NET. It has a column: temperature . I want to put picture of thermometer instead of writing temperature. I want this picture only for this column and not for the whole grid view. How do i do that?
This can be done easily in a GridView by using the HeaderImageUrl property on the column.
For Example:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderImageUrl="icon.png" />
<asp:TemplateField HeaderImageUrl="icon.png" />
</Columns>
</asp:GridView>
How can i add cell in specific column for each row in gridview,i want to use RowCreated Events.
i have gridview that has 3 columns (ProductName, Price, Count) i get (ProductName, Price) from database, and i want to add value for (count), ex: (Kitkat, 2$ ) i want to add number 5 to (count) column, i want to handle this operation when each row created.
Thanks
Since you haven't shown your markup, I'm going to assume (based on your comment) that the first two columns are <BoundFields>. If that's the case, I would add a third column as a <TemplateField>, place a Label in it, and use the RowDataBound event to add the correct number to the Label.
Here is what the markup would look like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:Label ID="countLbl" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code-behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label countLbl = (Label)e.Row.FindControl("countLbl");
//Your logic for what number to use should go here, I'm just defaulting to 5.
countLbl.Text = "5";
}
I'm using a GridView to display some ifnromation in ASP.NET. I need it to simply display 0 in the gridview fields (there's only ever one row returned) if the dataset is empty, plus of course display the headers. How do I do this?
Yes bind a dummy dataset with one row, with all zeros as fields, to the grid with zeros as the field labels. That would be the best way.
Bind your grid and add the EmptyDataTemplate like so:
<asp:GridView ID="gridview1" runat="server" ShowFooter="true">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID field" />
</Columns>
<EmptyDataTemplate>put whatever you want in here</EmptyDataTemplate>
</asp:GridView>
I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.
I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.
How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.
Please Help
Thank you.
Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :
<asp:gridview id="GridView1"
autogeneratecolumns="false"
runat="server">
<asp:HyperLinkField DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
DataTextField="UserName" />
</asp:gridview>
Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.
If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField
Let me know if I'm off the mark with this
here's some code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Whatever" />
<asp:TemplateField>
<ItemTemplate>
<a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>