Adding a column to a GridView at Runtime and DataFormatString

Discussion in 'ASP .Net' started by Eamonn, May 16, 2006.

  1. Eamonn

    Eamonn Guest

    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;
    Eamonn, May 16, 2006
    #1
    1. Advertising

  2. Eamonn

    PeterKellner Guest

    On 16 May 2006 05:18:31 -0700, "Eamonn" <>
    wrote:

    >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
    PeterKellner, May 16, 2006
    #2
    1. Advertising

  3. Eamonn

    Eamonn Guest

    Thanks Peter. I have tried both true and false. Neither has any effect.
    Eamonn, May 16, 2006
    #3
  4. Eamonn

    PeterKellner Guest

    On 16 May 2006 09:00:24 -0700, "Eamonn" <>
    wrote:

    >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
    PeterKellner, May 16, 2006
    #4
  5. Eamonn

    Eamonn Guest

    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;
    Eamonn, May 17, 2006
    #5
  6. Eamonn

    udayakumar86

    Joined:
    Nov 18, 2008
    Messages:
    1
    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
    udayakumar86, Nov 18, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Roberts
    Replies:
    3
    Views:
    22,171
    Martin Arredondo
    Apr 13, 2007
  2. Replies:
    2
    Views:
    541
  3. bsm
    Replies:
    3
    Views:
    703
    Ray Costanzo
    May 7, 2007
  4. Keith G Hicks
    Replies:
    3
    Views:
    763
    Keith G Hicks
    Feb 18, 2008
  5. cindy
    Replies:
    1
    Views:
    340
    Steven Cheng[MSFT]
    Apr 20, 2006
Loading...

Share This Page