在gridview标题中显示日期 [英] Dislpaying date in gridview header

查看:82
本文介绍了在gridview标题中显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有gridview,它没有数据,但应该有单行和5列。



在gridview上面我有一个显示日期的文本框,例如。如果我选择6月14日,那么gridview将在2013年6月12日,6月12日,6月13日和2013年6月13日生成。





日期应显示在gridview的标题中。我完成了,但问题是我的代码显示5行而不是1.



我需要这个: http://s22.postimg.org/47jpodc29/two.png [ ^ ]



但我明白了: http://s11.postimg.org/4l95njqsz/one.png [< a href =http://s11.postimg.org/4l95njqsz/one.pngtarget =_ blanktitle =新窗口> ^ ]



相同的代码如下:

I have gridview which has no data,but should have single row and 5 columns.

Above the gridview i have one textbox which displays date,e.g. if i select 14 june,then gridview will be generated with 10th june,11th june,12th june,13th june and 14th june 2013.....


The date should be displayed in header of the gridview.I accomplished that,but the problem is my code shows 5 rows rather then 1.

I need this:http://s22.postimg.org/47jpodc29/two.png[^]

but i am getting this: http://s11.postimg.org/4l95njqsz/one.png[^]

The code for the same is as follows:

protected void timegrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < dtab.Rows.Count; i++)
                {
                    e.Row.Cells[i].Text = dtab.Rows[i]["DatesofWeek"].ToString();
                }
            }
        }
        protected void BindEmptyGrid()
        {
            try
            {
                dtab = daNewTable();
                DateTime todate_ = Convert.ToDateTime(Session["1"]);
                DateTime fromdate_ = Convert.ToDateTime(Session["2"]);
                
                string[] weekdates = new string[5];
                for (DateTime date1 = fromdate_ ;date1 <= todate_ ;date1=date1.AddDays(1))
                {
                        DataRow d = dtab.NewRow();
                        d["DatesofWeek"] = ChangeFormat(Convert.ToDateTime(date1), "dd-MM-yyyy");
                        dtab.Rows.Add(d);
                }
                
                if (weekdates.Length > 0)
                {
                    timegrid.DataSource = dtab;
                    timegrid.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataTable daNewTable()
        {
            dtempty = new DataTable();
            dtempty.Columns.Add("DatesofWeek");
            return dtempty;
        }





网格代码:





我无法理解应该采取什么修改来解决这个问题。



任何帮助都会非常感激...



Gridview code:


I can not understand what modification should be done to overcome the issue.

Any help would be really appreciated...

推荐答案

试试这个...



try this...

protected void timegrid_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           //if (e.Row.RowType == DataControlRowType.Header)
           //{
           //    for (int i = 0; i < dtab.Rows.Count-1; i++)
           //    {
           //        e.Row.Cells[i].Text = dtab.Rows[i]["DatesofWeek"].ToString();
           //    }
           //}
       }
       DataTable dtab;
       protected void BindEmptyGrid()
       {
           try
           {
               dtab = daNewTable();
               DateTime todate_ = Convert.ToDateTime("14-06-2013");
               DateTime fromdate_ = Convert.ToDateTime("10-06-2013");

               string[] weekdates = new string[5];
               DataRow d = dtab.NewRow();
               for (DateTime date1 = fromdate_; date1 <= todate_; date1 = date1.AddDays(1))
               {
                   DataColumn dc = new DataColumn();
                   dc.ColumnName  = Convert.ToDateTime(date1).ToString("dd-MM-yyyy");
                   dtab.Columns.Add(dc);


               }
               dtab.Rows.Add(d);
               if (weekdates.Length > 0)
               {
                   timegrid.DataSource = dtab;
                   timegrid.DataBind();
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
       DataTable dtempty;
       public DataTable daNewTable()
       {
           dtempty = new DataTable();
           //dtempty.Columns.Add("DatesofWeek");
           return dtempty;
       }


这篇关于在gridview标题中显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆