如何在gridview的标题中更改日期格式 [英] How to change date format in header of gridview

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

问题描述

大家好我正在使用gridview显示数据,其中我在标题中使用了日期,如01,02,03,04,05到月底,并且工具提示也用于在用户将鼠标移到其上时显示数据描述,但是最后,当我看到它在标题中以yyyy-mm-dd格式显示日期,但我只希望日期没有月份或年份

Hi everyone i am using gridview to display data in which i have used date in header like 01,02,03,04,05 upto end of month and tooltip also used to display data description whenever user move mouse over it, but at end when i see it display date in header in format yyyy-mm-dd but i want only date no month or year

if (e.Row.RowType == DataControlRowType.DataRow)
       {




               for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
               {
                   if (e.Row.Cells[i].Text == "0" || string.IsNullOrEmpty(e.Row.Cells[i].Text) || e.Row.Cells[i].Text == " ")
                   {
                       e.Row.Cells[i].Text = "";
                   }
                   else
                   {
                       e.Row.Cells[i].BackColor = System.Drawing.Color.DarkOrange;

                       dateSetExport.Tables.Clear();
                       dateSetExport.Reset();
                       SqlParameter[] param = new SqlParameter[2];
                       param[0] = new SqlParameter("@Employe_Id", e.Row.Cells[0].Text.Split('-')[0]);
                       param[1] = new SqlParameter("@Startdate", gvDetails.HeaderRow.Cells[i].Text);
                       DataTable dt1 = DataHelper.getDataTableExecuteSP("usp_GetToolTip", param);
                       dt1.TableName = "ToolTip";
                       dateSetExport.Tables.Add(dt1);
                       string tooltip = "";
                       for (int j = 0; j < dt1.Rows.Count; j++)
                       {
                           tooltip = tooltip + dt1.Rows[j]["normal_working_hours"].ToString() + " Hours : " + dt1.Rows[j]["description"].ToString() + "\n\n";

                       }
                       e.Row.Cells[i].ToolTip = tooltip;

                   }

                   //date = date.Split('-')[0] + "-" + date.Split('-')[1] + "-" + Convert.ToString(Convert.ToInt32(date.Split('-')[2]) + 1);
                   //if(date=="2017-07-31")
                   //{
                   //    date = date.Split('-')[0] + "-" + Convert.ToString(Convert.ToInt32(date.Split('-')[1])+1)+ "-" + Convert.ToString(Convert.ToInt32(date.Split('-')[2]) -30);
                   //}
               }


       }


       e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;

       if (e.Row.RowIndex == 1)
       {
           //Creating a gridview object
           GridView objGridView = (GridView)sender;

           //Creating a gridview row object
           GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

           //Creating a table cell object
           TableHeaderCell objtablecell = new TableHeaderCell();

           foreach (TableCell tblCell in e.Row.Cells)
           {
               objtablecell = new TableHeaderCell();
               objgridviewrow.Cells.Add(objtablecell);
           }

           objgridviewrow.ID = "NewHeader";
           objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0xf0, 0x9b, 0x16);
           objgridviewrow.ForeColor = System.Drawing.Color.Black;
           objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);


           GridViewRow firstRow = (GridViewRow)gvDetails.FindControl("NewHeader");
           GridViewRow secondRow = gvDetails.HeaderRow;

           for (int i = secondRow.Cells.Count - 2; i >= 0; i--)
           {
               if (secondRow.Cells[i].Text.Substring(5,2) == secondRow.Cells[i + 1].Text.Substring(5,2))
               {
                   firstRow.Cells[i].ColumnSpan = firstRow.Cells[i+1].ColumnSpan < 2 ? 2 : firstRow.Cells[i+1].ColumnSpan + 1;
                   firstRow.Cells[i + 1].Visible = false;

                   firstRow.Cells[i].Text = CellText(secondRow.Cells[i].Text.Substring(5, 2)) + " " + secondRow.Cells[i].Text.Substring(0, 4);
                   firstRow.Cells[i].HorizontalAlign = HorizontalAlign.Center;

               }

               if (i== 1 && (firstRow.Cells[1].Text==""))
               {
                   firstRow.Cells[1].Text = CellText(secondRow.Cells[1].Text.Substring(5, 2)) + " " + secondRow.Cells[1].Text.Substring(0, 4);
                   firstRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
               }
               else if (i == 0 && (firstRow.Cells[firstRow.Cells.Count - 1].Text == ""))
               {
                   string fullText = CellText(secondRow.Cells[secondRow.Cells.Count - 1].Text.Substring(5, 2)) + " " + secondRow.Cells[secondRow.Cells.Count - 1].Text.Substring(0, 4);

                   firstRow.Cells[firstRow.Cells.Count - 1].Text = fullText;

                   firstRow.Cells[firstRow.Cells.Count - 1].HorizontalAlign = HorizontalAlign.Center;
               }

           }

           //foreach (TableCell tblCell in gvDetails.HeaderRow.Cells)
           //{
           //    if (tblCell.Text != "Employee Name")
           //    {
           //        tblCell.Text = tblCell.Text.Substring(8, 2);
           //    }
           //}
       }
   }



这是我的网格


This is my grid

<asp:GridView ID="gvDetails" runat="server" Style="font-size: 13px; width: 100%;" CssClass="grid clsWrap" AutoGenerateColumns="True" OnRowDataBound="gvDetails_RowDataBound">

       <HeaderStyle Font-Bold="False" CssClass="Font gvheaderstyle" Wrap="False"/>
       <PagerStyle CssClass="gridB" ForeColor="WhiteSmoke" />
       <RowStyle HorizontalAlign="Center" />
   </asp:GridView>





我的尝试:





What I have tried:

//foreach (TableCell tblCell in gvDetails.HeaderRow.Cells)
           //{
           //    if (tblCell.Text != "Employee Name")
           //    {
           //        tblCell.Text = tblCell.Text.Substring(8, 2);
           //    }
           //}





每个转换日期的格式为i想但是如果我取消注释这个foreach它会影响每一行的工具提示,如果我在gridview的标题中注释它的效果日期会导致读取一行的数据yyyy-mm-dd但我只想要一天



帮帮我



this for each converts date in the format i want but if i uncomment this foreach it effect tooltip in each row that result in reading data of one row if i comment it effect date in header of gridview results in yyyy-mm-dd but i want only day

help me

推荐答案

你的要求没有清楚提到。在网格视图中你设置了auto generation columns true
ur requirement isn't mentioned clearly.in grid view u r set the auto generation columns true


您应该在GridView中使用自定义生成列,而不是使用自动生成的列。对于我的表中的示例,我有两列Date和Name。这就是我要做的事情

Instead of using Autogenerated Columns you should use custom generation of Columns in GridView. For Example in my table i have two columns Date and Name. This what i am gonna do
<asp:GridView ID="gvDetails" runat="server" Style="font-size: 13px; width: 100%;" CssClass="grid clsWrap" AutoGenerateColumns="True" OnRowDataBound="gvDetails_RowDataBound">
 
        <HeaderStyle Font-Bold="False" CssClass="Font gvheaderstyle" Wrap="False"/>
        <PagerStyle CssClass="gridB" ForeColor="WhiteSmoke" />
        <RowStyle HorizontalAlign="Center" />
<Columns>                            
                            <asp:BoundField DataField="Date" DataFormatString="{0:MM/dd/yyyy}"  HeaderText="Order Date" >                               
                                 
                            </asp:BoundField>
                            <asp:BoundField DataField="Name" HeaderText="Name" >
                                
                            </asp:BoundField>
    </asp:GridView>





请注意DataFied =Date的第一个Bound字段具有DataFormat String的属性。你可以改变你想要的结果。



Note the first Bound field with DataFied="Date" has property of DataFormat String. you can change for your desired result.


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

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