Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
F

Farshid Zavareh

@Farshid Zavareh
About
Posts
2
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • DateFormat problem
    F Farshid Zavareh

    This is what I usually do: The code should be executed after DataGrid is data bound(DataGrid.DataBind() is called). So that it can edit DataGrid items. In post backs, you don't need to data bind DataGrid, because it is already data bound (in load), but you may need to run converting code in post backs, too. To do so, data bind DataGrid in page_load (use if(!IsPostBack)). Place code for modifying DataGrid in a separate method and call it in page_load (outside if(!IsPostBack)). If you are using ASP.NET 2, note that when you do data binding using a DataSource, you still need to call DataGrid.DataBind() in page_load. Because this method is called after page_load in page life cycle and you need to work on DataGrid in page_load, you must call it explicitly. But using ItemDataBound, it is much better and easier:

    protected void dgrdWebSites\_ItemDataBound(object s, DataGridItemEventArgs e)
    {
        DateTime date = DateTime.Parse(e.Item.Cells\[1\].Text);
        string text = date.Day + "/" + date.Month + "/" +
            date.Year;
        e.Item.Cells\[1\].Text = text;
    }
    

    Nothing more is needed! Let me know if you need more explanation. Best regards, lordfkiller

    ASP.NET database help sql-server sysadmin question

  • DateFormat problem
    F Farshid Zavareh

    I don't know how you can do date conversion in SQL, but you can use the following code to do this in page code:

    // Language = C#
    // dgrd = DataGrid
    // change cell index to cell index for your datagrid
    
    foreach (DataGridItem item in dgrd.Items)
    {
        DateTime date = DateTime.Parse(item.Cells\[1\].Text);
        item.Cells\[1\].Text = date.ToShortDateString();
    }
    

    This sould work, but if result is not in dd/mm/yyyy format then use the following:

    // Language = C#
    // dgrd = DataGrid
    // change cell index to cell index for your datagrid
    
    foreach (DataGridItem item in dgrd.Items)
    {
        DateTime date = DateTime.Parse(item.Cells\[1\].Text);
        string text = date.Day + "/" + date.Month + "/" +
            date.Year;
        item.Cells\[1\].Text = text;
    }
    

    Just make sure data type for the column in database is datetime. If it is not, use a try...catch loop when parsing string to prevent error. Regards, lordfkiller

    ASP.NET database help sql-server sysadmin question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups