Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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...
Posted
Updated 13-Jun-13 1:21am
v7

1 solution

try this...

C#
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;
       }
 
Share this answer
 
v2
Comments
Sejal Rabari 13-Jun-13 6:44am    
Good one the 5 rows issue has gone,but i am not getting dates in header,please have a look at this image snapshot
yourfriendaks 13-Jun-13 7:03am    
R U USING ITEMTEMPLATE WITHIN GRID
Sejal Rabari 13-Jun-13 7:19am    
yes
Sejal Rabari 13-Jun-13 7:21am    
<asp:GridView ID="timegrid" runat="server" OnRowDataBound="timegrid_RowDataBound"
AutoGenerateColumns="false">
<columns> <asp:TemplateField HeaderText="ID" SortExpression="ID">
<HeaderTemplate>
<asp:Label ID="lbl1" runat="server" Style="text-align: right;" Width="80px" />
</HeaderTemplate>
<itemtemplate>
<asp:TextBox ID="hour1" runat="server" Text="">

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter Number Only"
ControlToValidate="hour1" ValidationExpression="^[0-9]\d*(\.\d+)?$">Enter Numbers Only


<asp:TemplateField HeaderText="Redirect">
<HeaderTemplate>
<asp:Label ID="lbl1" runat="server" Style="text-align: right;" Width="80px" />
</HeaderTemplate>
<itemtemplate>
<asp:TextBox ID="hour2" runat="server" Text="">

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Enter Number Only"
ControlToValidate="hour2" ValidationExpression="^[0-9]\d*(\.\d+)?$">Enter Numbers Only


<asp:TemplateField HeaderText="Quantity">
<HeaderTemplate>
<asp:Label ID="lbl1" runat="server" Style="text-align: right;" Width="80px" />
</HeaderTemplate>
<itemtemplate>
<asp:TextBox ID="hour3" runat="server" Text="">

<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Enter Number Only"
ControlToValidate="hour3" ValidationExpression="^[0-9]\d*(\.\d+)?$">Enter Numbers Only


<asp:TemplateField HeaderText="Cost Each">
<HeaderTemplate>
<asp:Label ID="lbl1" runat="server" Style="text-align: right;" Width="80px" />
</HeaderTemplate>
<itemtemplate>
<asp:TextBox ID="hour4" runat="server" Text="">

<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Enter Number Only"
ControlToValidate="hour4" ValidationExpression="^[0-9]\d*(\.\d+)?$">Enter Numbers Only


<asp:TemplateField HeaderText="Amount">
<HeaderTemplate>
<asp:Label ID="lbl1" runat="server" Style="text-align: right;" Width="80px" />
Sejal Rabari 13-Jun-13 7:50am    
i removed all the templatefields and i am getting output,but without textbox,now issue is for only textbox

refer this link for latest output

http://s20.postimg.org/afhrcjzel/latest.png

in that single row i aimed to have textbox....

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