Doubts about column formatting in gridView

Asked

Viewed 4,108 times

3

Hi, I have some questions about formatting the columns in a gridview, I have a gridView that is populated by data coming from a table in the database, the columns and row are generated automatically, I have a method that performs the query in the 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 which displays the information to 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 doubt is how I can 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 to edit the columns but as they are automatically generated they do not appear to be formatted as shown below: inserir a descrição da imagem aqui can I add Boundfield type columns and format them? even if my gridview generates columns automatically? if it is possible I can form in the way I need but if it is not possible there is some other way?

1 answer

3


Yes, there is that possibility, Dataformatstring, having there numerous configurations of data format.

Class Fulano

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 it on Gridview AutoGenerateColumns="False" when you manually configure all fields (Edit Columns).

inserir a descrição da imagem aqui

Configuring the Date field:

inserir a descrição da imagem aqui

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 shortened date, and DataField="Valor" one DataFormatString="{0:N2}" which is number to two decimal places, if you need to add more boxes just increase 2 to the corresponding number.

Reminder:

Also set up your Globalization for en in his web.config which is the Brazilian standard of currency, date, etc.

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

Upshot:

inserir a descrição da imagem aqui

In that link has the formats, so, it is based on this table.

Reference:

  • if by Autogeneratecolumns="False", I have to add the columns via edit columns I’m not sure ?

  • If you put Autogeneratecolumns="False", then you add the columns manually, in the Add Column in the arrow beside the Gridview

  • One more doubt the data will come from the database in the configuration of the first question method, this data remains correct ? just add the right amount of column

  • This, now you must relate, the data that comes from the bank with those that will appear in Gridview. Example: has 10 fields coming from the bank are 10 Columns in your Gridview, with the formatting in the fields you need, the ones you don’t need DataFormatString.

  • Ok solved, thanks for the help, just a hint if possible with do to change the colors of the font?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.