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: 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?
if by Autogeneratecolumns="False", I have to add the columns via edit columns I’m not sure ?
– user9090
If you put Autogeneratecolumns="False", then you add the columns manually, in the Add Column in the arrow beside the Gridview
– user6026
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
– user9090
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 needDataFormatString
.– user6026
Ok solved, thanks for the help, just a hint if possible with do to change the colors of the font?
– user9090