Click here to Skip to main content
16,010,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
           //    }
           //}


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
Posted
Updated 7-Sep-17 0:58am
Comments
Kornfeld Eliyahu Peter 5-Sep-17 6:42am    
I do not think you can do it with auto-generated columns, you have to create your own templates for headers and columns and add format string there...

ur requirement isn't mentioned clearly.in grid view u r set the auto generation columns true
 
Share this answer
 
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.NET
<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>


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900