You must Sign In to post a response.
  • Category: ASP.NET

    Last row data is not shown in excel showing in gridview using asp.net

    in my gridview lastrow data is showing but in excel it is not showing
  • #762394

    gridviewprnt.bmp

    Delete Attachment

  • #762404
    Hi selvi,

    I have seen your all posts, you haven't provide full details of your requirement which is not good, this won't helpful to you to resolve your problem.

    Coming to your query, I'm not sure about your code logic and requirement but as per my understand the thread I guess the problem might be data reading or excel formatting. I request you to please debug the code and check in which line it's getting problem.

    Hope you understood.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762406
    Hi

    Can you explain more your Question Excel data showing so your records is showing what is the issue? can you elaborate more. your image i canot identify.

    What is your issue from excel sheet records First row or last row

    you canot bind in your gridview?

    I cant understood in your issue explain more.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762414
    my source

    <asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
    <table class="style1" align="center" style="border: thin outset #1d599e; width: 100%; margin-left: 0px; background-color:White;">
    <tr><td style="margin-left: 80px; background:#1d599e;" colspan="2" align="center" >
    <asp:Label ID="Label17" runat="server" Text="Employee Details"
    Style="color: #FFFFFF; font-size: large; font-weight: 700; text-align: center;"
    class="style10"></asp:Label></td></tr>
    <tr><td>
    <asp:Label ID="Label25" runat="server" Text="ID"></asp:Label>
    </td><td>
    <asp:TextBox ID="vewid" runat="server" ontextchanged="vewid_TextChanged"></asp:TextBox>
    </td></tr>
    <tr><td>
    <asp:Label ID="ffdate" runat="server" Text="From Date"></asp:Label>
    </td><td>
    <asp:TextBox ID="txtff" runat="server" AutoPostBack="True"
    ontextchanged="txtff_TextChanged"></asp:TextBox>
    <asp:Calendar ID="ffdar" runat="server"
    onselectionchanged="ffdar_SelectionChanged"></asp:Calendar>
    <asp:TextBox ID="ffdatetxt" runat="server" Visible="False"></asp:TextBox>
    </td></tr>
    <tr><td>
    <asp:Label ID="Label26" runat="server" Text="To Date"></asp:Label>
    </td><td>
    <asp:TextBox ID="txttodate" runat="server" AutoPostBack="True"
    ontextchanged="txttodate_TextChanged"></asp:TextBox>
    <asp:Calendar ID="toddr" runat="server"
    onselectionchanged="toddr_SelectionChanged"></asp:Calendar>
    <asp:TextBox ID="todatetxt" runat="server" Visible="False"></asp:TextBox>
    <br />
    </td></tr>
    <tr><td colspan="2" align="center">
    <asp:Button ID="b11" runat="server" onclick="b11_Click" Text="Button"
    Width="302px" />
    </td></tr> </table>
    </asp:View>

    <asp:View ID="View2" runat="server">
    <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC"
    BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
    CellSpacing="2" ForeColor="Black" AutoGenerateColumns="false" Width="500px"
    ShowFooter="True">
    <RowStyle BackColor="White" /> <FooterStyle BackColor="Black" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" /><PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <Columns>
    <asp:TemplateField HeaderText="Name" ControlStyle-Width="120px"><ItemTemplate>
    <asp:Label ID="lsect" runat="server" Text='<%#Eval("Name")%>'></asp:Label></ItemTemplate>
    <ControlStyle Width="120px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Date" ControlStyle-Width="120px"><ItemTemplate> <asp:Label ID="lexpen" runat="server" Text='<%#Eval("Date")%>'></asp:Label></ItemTemplate>
    <ControlStyle Width="120px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Status" ControlStyle-Width="120px"><ItemTemplate> <asp:Label ID="ldate" runat="server" Text='<%#Eval("Status") %>'></asp:Label></ItemTemplate>
    <ControlStyle Width="120px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Intime" ControlStyle-Width="120px"><ItemTemplate> <asp:Label ID="lbno" runat="server" Text='<%#Eval("Intime") %>'></asp:Label></ItemTemplate>
    <ControlStyle Width="120px" />
    </asp:TemplateField>


    <asp:TemplateField HeaderText="Outtime">
    <ItemTemplate>
    <asp:Label ID="lblSalary" runat="server" Text='<%# Eval("Outtime")%>' />
    </ItemTemplate>
    <FooterTemplate>
    <asp:Label ID="Salary" runat="server" />
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>


    </asp:GridView>

    <asp:Button ID="Button1" runat="server" Text="Next" onclick="Button1_Click"
    Width="328px" />
    </asp:View>
    </asp:MultiView>


    my code

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;
    using System.Text;

    public partial class viewexpenses : System.Web.UI.Page
    {
    string s = ConfigurationManager.AppSettings["s"].ToString();
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    DataSet ds = new DataSet();
    Marvel mvl = new Marvel();
    //int m = 0;
    double m = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    MultiView1.SetActiveView(View1);
    }
    }
    private void supdet()
    {
    try
    {
    string sekk = "BM";
    string qe = "select * from Expenzsection where date='" + Session["todate"].ToString() + "'order by expensesname";

    DataSet dss = new DataSet();
    dss = mvl.GETDS(qe);
    if (dss.Tables[0].Rows.Count != 0)
    {
    GridView1.DataSource = dss;
    GridView1.DataBind();
    }
    else if (dss.Tables[0].Rows.Count == 0)
    {
    Response.Write("<script>alert('Please Enter Bill Amount !')</script>");
    }
    else
    {
    Response.Write("<script>alert('Please Entercorrect date !')</script>");
    }
    }
    catch (Exception e)
    {
    throw e;
    }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    Session["todate"] = TextBox1.Text;
    MultiView1.SetActiveView(View2);
    supdet();
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
    TextBox1.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
    Calendar1.Visible = true;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    MultiView1.SetActiveView(View1);
    TextBox1.Text = "";
    Calendar1.SelectedDates.Remove(Calendar1.SelectedDates[0]);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    Label Salary = (Label)e.Row.FindControl("lblSalary");
    //m = m + int.Parse(Salary.Text);
    m = m + double.Parse(Salary.Text);
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
    Label lblTotalPrice = (Label)e.Row.FindControl("Salary");
    lblTotalPrice.ForeColor = System.Drawing.Color.Black;
    lblTotalPrice.Text = m.ToString();
    }
    if (e.Row.RowType == DataControlRowType.Header)
    {
    GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Insert);
    TableCell th = new TableHeaderCell();
    //th.ForeColor = Color.White;
    //th.BackColor = Color.SteelBlue;
    th.Text = "BFS<br/> DEvakottai<br/>         Expenses Details on ('" + Session["todate"] + "')";
    th.ColumnSpan = GridView1.Columns.Count;
    row.Cells.Add(th);
    GridView1.Controls[0].Controls.AddAt(0, row);
    }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    /* Verifies that the control is rendered */
    }
    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";

    GridView1.AllowPaging = false;
    GridView1.DataBind();

    StringBuilder sb = new StringBuilder();
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
    //add separator
    sb.Append(GridView1.Columns[k].HeaderText + ',');
    }
    //append new line
    sb.Append("\r\n");
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
    //add separator
    sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
    }
    //append new line
    sb.Append("\r\n");
    }
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();
    }
    protected void btnExportWord_Click(object sender, EventArgs e)
    {
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition","attachment;filename=Test1.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word ";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.RenderControl(hw);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();

    }

    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
    "attachment;filename=Test1.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel ";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.RenderControl(hw);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
    }

    protected void btnExportPDF_Click(object sender, EventArgs e)
    {
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
    }
    protected void btnExportCSV_Click(object sender, EventArgs e)
    {
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";

    GridView1.AllowPaging = false;
    GridView1.DataBind();

    StringBuilder sb = new StringBuilder();
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
    //add separator
    sb.Append(GridView1.Columns[k].HeaderText + ',');
    }
    //append new line
    sb.Append("\r\n");
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
    //add separator
    sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
    }
    //append new line
    sb.Append("\r\n");
    }
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();
    }
    }

  • #762417
    Hi

    You can try this code working for me
    Generated all column



    Response.Clear();
    Response.Buffer = true;

    Response.AddHeader("content-disposition",
    "attachment;filename=Test1.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel ";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    Grd1.RenderControl(hw);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();


    Then if you missing last row means


    You must changed in your for loop update this

    k <= GridView1.Columns.Count-1


    for (int k = 0; k <= GridView1.Columns.Count-1; k++)
    {
    //add separator
    sb.Append(GridView1.Columns[k].HeaderText + ',');
    }



    I have attached snapshot given below

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #762426
    same mistake


Sign In to post your comments