Questions about column formatting in the gridView

3
Hi, I have some questions about formatting columns in a gridview, I have a gridview that populated by data coming from a table in the database, the columns and row are automatically generated, I have a method that performs the query in the gridview. database:

public GridView exibeCarteira(string cepf, ref GridView tb)
    {
        try
        {
            bancotccEntities bc = new bancotccEntities();

            var crt = from cart in bc.carteira
                      where cart.cpf == cepf
                      select new
                      {
                          Codigo = cart.codigo,
                          Valor = cart.valoracao,
                          Quantidade = cart.qtdacao,
                          Total = cart.vtotalacao,
                          Valor_Gasto = cart.vinvestido

                      };
            tb.DataSource = crt.ToList() ;
            tb.DataBind();
            return tb;
        }
        catch (Exception e1)
        {
            throw new Exception(e1.Message.ToString());

        }

    }

This method is called by the method:

public GridView mostraCarteira(string cpf, ref GridView gv)
    {
        try
        {
            ManipulaBanco mp = new ManipulaBanco();
            return mp.exibeCarteira(cpf, ref  gv);
        }
        catch (Exception e4)
        {

            throw new Exception(e4.Message.ToString());
        }
    }

Which is called by the method of my gridview that displays information for the user

 public partial class ExibeCarteira : System.Web.UI.Page
{
    string cpf;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                exibirCarteira();
            }
        }
        catch (Exception ex)
        {

            throw new Exception(ex.Message.ToString());
        }
    }
    private void exibirCarteira()
    {
        try
        {
            cpf = "98765432101";
            Trataformes tf = new Trataformes();
            this.gvcarteira = tf.mostraCarteira(cpf, ref gvcarteira);

        }
        catch (Exception e1)
        {

            throw new Exception(e1.Message.ToString());
        }
    }
    protected void gvcarteira_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

}

My question is how can I format the information that will be displayed in each column in formats like date, money, set the amount of 0 after the comma, etc. I tried editing the columns but as they are generated automatically they do not appear to be formatted as shown below:  can I add BoundField type columns and format them? even though my gridview automatically manages the columns? if it is possible I can format it in the way I need it but if it is not possible it has some other way?

    
asked by anonymous 15.06.2014 / 22:14

1 answer

3

Yes, there is this possibility, at DataFormatString , having numerous data format settings.

Class

public class Fulano
{
    public int Codigo { get; set; }
    public string Nome { get; set; }
    public DateTime Data { get; set; }
    public Decimal Valor { get; set; }
}

GridView

Put in Gridview AutoGenerateColumns="False" when you manually configure all fields (Edit Columns).

SettingtheDatefield:

Code generated after all settings:

<asp:GridView ID="GridDados" AutoGenerateColumns="False" runat="server" ItemType="WebAppDiegoWebForms.Fulano">
    <Columns>
        <asp:BoundField DataField="Codigo" HeaderText="Código" DataFormatString="{0:0000}"/>
        <asp:BoundField DataField="Nome" HeaderText="Nome" />
        <asp:BoundField DataField="Data" DataFormatString="{0:d}" HeaderText="Data" />
        <asp:BoundField DataField="Valor" DataFormatString="{0:N2}" HeaderText="Valor" />
    </Columns>
</asp:GridView>

The DataField="Data" has a DataFormatString="{0:d}" which would be the abbreviated date, and the DataField="Valor" a DataFormatString="{0:N2}" which is a number with two decimal places, if you need to add more houses just increase the 2 to the corresponding number.

Reminder:

Also set your Globalization to en in your web.config which is the Brazilian default currency, date, etc.

<configuration>
    <system.web>
        <globalization culture="pt-BR" uiCulture="pt-BR" />
    </system.web>
</configuration>

Result:

Inthis link has the formats , then it is based on this table.

Reference:

15.06.2014 / 23:25