Adding a column to a GridView at Runtime and DataFormatString

E

Eamonn

Hi

I am running Microsoft Visual Studio 2005 Team Edition Version
8.0.50727.42. I am adding columns to a GridView at runtime. When I add
a BoundField that will contain DateTime data I want to format the date
so I use:
BoundField.DataFormatString = <myDateFormat>

However it just displays the date formatting string in the GridView and
not the formatted date. I tried setting the HtmlEncode value for the
column to both true and false, but it makes no difference. A snippet of
my code is below. Any help would be appreciated.

Thanks

Eamonn

//BoundField
case (int)DSSCaseColumn.DataControlFields.BoundField:

BoundField boundField = new BoundField();
boundField.DataField = column.DatabaseFieldName;
boundField.HeaderText = column.HeaderText;
SetFieldWidth(boundField, column.PercentageColumnWidth);
if (column.IsDate == true)
{
boundField.HtmlEncode = true;
boundField.DataFormatString =
Constants.GRIDVIEW_DATE_DISPLAY_FORMAT.ToString();
}
SelectGridViewCases.Columns.Add(boundField);
break;
 
P

PeterKellner

Hi

I am running Microsoft Visual Studio 2005 Team Edition Version
8.0.50727.42. I am adding columns to a GridView at runtime. When I add
a BoundField that will contain DateTime data I want to format the date
so I use:
BoundField.DataFormatString = <myDateFormat>

However it just displays the date formatting string in the GridView and
not the formatted date. I tried setting the HtmlEncode value for the
column to both true and false, but it makes no difference. A snippet of
my code is below. Any help would be appreciated.

Thanks

Eamonn

//BoundField
case (int)DSSCaseColumn.DataControlFields.BoundField:

BoundField boundField = new BoundField();
boundField.DataField = column.DatabaseFieldName;
boundField.HeaderText = column.HeaderText;
SetFieldWidth(boundField, column.PercentageColumnWidth);
if (column.IsDate == true)
{
boundField.HtmlEncode = true;
boundField.DataFormatString =
Constants.GRIDVIEW_DATE_DISPLAY_FORMAT.ToString();
}
SelectGridViewCases.Columns.Add(boundField);
break;


You want to set:

boundField.HtmlEncode = false;

(not true)

Peter Kellner
http://peterkellner.net
Peter Kellner
http://peterkellner.net
 
P

PeterKellner

Thanks Peter. I have tried both true and false. Neither has any effect.

Very strange. Here is a small example I put together that works as I
describe. Give it a try and see what happens. (BirthDate is of
course a data column in my data object.

public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
BoundField boundField = new BoundField();
boundField.DataField = "BirthDate";
boundField.HeaderText = "BirthDateHeader";
boundField.HtmlEncode = false;
boundField.DataFormatString = "{0:d}";
GridView1.Columns.Add(boundField);
}
}
Peter Kellner
http://peterkellner.net
 
E

Eamonn

Hi Peter

Thanks to your example I spotted the issue. I needed to add the curly
braces { and } to the formatting string along with the ordinal
parameter. The following code now works fine:

BoundField boundField = new BoundField();
boundField.DataField = column.DatabaseFieldName;
boundField.HeaderText = column.HeaderText;
SetFieldWidth(boundField, column.PercentageColumnWidth);
if (column.IsDate == true)
{
boundField.HtmlEncode = false;
boundField.DataFormatString = "{0:" +
Constants.GRIDVIEW_DATE_DISPLAY_FORMAT.ToString() + "}";
}
SelectGridViewCases.Columns.Add(boundField);
break;
 
Joined
Nov 18, 2008
Messages
1
Reaction score
0
Pageindex problem

hi

I am running visual studio 2005 Professional editon.
I am adding column to gridview at runtime.
i am used PageIndex. The problem is when i am changing oone page another page column get adding. on first changing one column, on second changing one more gets added so i get two column with same name,


this is my coding
public void sch()
{

DateTime tempdate = DateTime.Parse(txtindidate.Text);
string day = tempdate.ToString("dd");
string mon=tempdate.ToString("MM");
string year = tempdate.ToString("yyyy");
string sel = "Select s" + day + ",id,name,desig from staffatten where mon='" + mon + "' and year='" + year + "'";
OdbcConnection con = new OdbcConnection("dsn=udaya;uid=sa;pwd=sa;");
OdbcCommand cmd = new OdbcCommand(sel, con);
con.Open();
OdbcDataReader dr = cmd.ExecuteReader();



BoundField field = new BoundField();
field.DataField = "s" + day + "";
field.HeaderText = "Status";
DataControlField col1 = field;
GridView1.Columns.Add(col1);

if (dr.Read())
{
con.Close();
Label2.Visible = false;
OdbcDataAdapter da = new OdbcDataAdapter(sel, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.Visible = true;
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
Label2.Visible = true;
}
}
protected void Button1_Click(object sender, EventArgs e)
{

sch();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
sch();
}

what i want to do on this
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top