Click here to Skip to main content
15,892,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m converting the gridview data into the excel sheet at run time.
my gridview has some linkbutton that should not be shown in the excel sheet.i have done that but the rows that has that linkbuttons are showing in the sheet can we remove that rows when converting that into excel sheet. help me?
Posted
Comments
Prerak Patel 10-Nov-11 1:50am    
Yes you can. Just skip the row when you check for the link button. Share the relevant code if you are stuck somewhere.
Member 8387468 10-Nov-11 1:51am    
I M USING THIS CODE TO CONVERT:-
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
try
{
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv_Employes.AllowPaging = false;
// gv_Employes.DataBind();
//Change the Header Row back to white color
gv_Employes.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gv_Employes.HeaderRow.Cells.Count; i++)
{
gv_Employes.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in gv_Employes.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gv_Employes.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
foreach (GridViewRow row in gv_Employes.Rows)
{
LinkButton lbtnview = (LinkButton)row.FindControl("lbtnview");
LinkButton lbtnedit = (LinkButton)row.FindControl("lbtn_edit_emp");
LinkButton lbtndel = (LinkButton)row.FindControl("lbtnsearchdel");
lbtnview.Visible = false;
lbtnedit.Visible = false;
lbtndel.Visible = false;
}
gv_Employes.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}catch{}


}
Member 8387468 10-Nov-11 1:51am    
HOW CAN I SKIP THAT ROWS???//
Member 8387468 10-Nov-11 2:08am    
sir do u have any ans for this query????

This is what i did in my code to hide columns, might help you
private void ExportMyAppointmentasCSV(GridView grdGridView, int clinicianID)
    {
        DataSet ds = new DataSet();
        clsAppointmentDb objAppTypeDb = new clsAppointmentDb();
        ds = objAppTypeDb.GetAppointmentsbyClinician(clinicianID);
        grdGridView.AllowPaging = false;
        grdGridView.PageIndex = 0;
        grdGridView.DataSource = ds.Tables[0];
        grdGridView.DataBind();
        if (grdGridView.Rows.Count > 0)
        {
            grdGridView.GridLines = GridLines.Both;
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=MyClinic.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xls";
            //For removing the Hyperlink from the Header of Grid View Before Writing to Excel
            if (grdGridView.HeaderRow != null)
            {
                PrepareControlForExport(grdGridView.HeaderRow);
            }
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            //This is Used to remove the columns like check box and Edit Delete coloumn
            grdGridView.Columns[8].Visible = false;
            grdGridView.Columns[0].Visible = false;
            grdGridView.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.Flush();
            Response.End();
        }
 
Share this answer
 
Comments
Member 8387468 10-Nov-11 4:32am    
thanx a lot.i just add 2 lines in my code and its working...
that are grdGridView.Columns[10].Visible = false; and the columns which i wanted to invisible.
Anuja Pawar Indore 10-Nov-11 4:37am    
Welcome :)
Code behind the export button


VB
Protected Sub cmddown_Click_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmddown.Click

        '#####ALL DATA####
        dg.Columns(0).Visible = False
        Dim form As HtmlForm = New HtmlForm
        Dim attachment As String = "attachment; filename=Employee.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/ms-excel"
        Dim stw As System.IO.StringWriter = New System.IO.StringWriter
        Dim htextw As HtmlTextWriter = New HtmlTextWriter(stw)
        form.Controls.Add(dg)
        Me.Controls.Add(form)
        DisableControls(dg)
        form.RenderControl(htextw)
        Response.Write(stw.ToString)
        Response.End()
        '#####ALL DATA####

        ''#### ORI ####
        'Response.Clear()
        'Dim filename As String = "filename_" & Strings.Format(DateTime.Today, "MM-dd-yyyy") & ".xls"
        'Response.Clear()
        'Response.AddHeader("content-disposition", "attachment; filename=" & filename)
        'Response.Charset = ""
        'Response.ContentType = "application/vnd.xls"
        'Me.EnableViewState = False
        'Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
        'Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
        'Response.Buffer = False
        ''ClearControls(dg)
        'dg.RenderControl(htmlWrite)
        'Response.Write(stringWrite.ToString())
        'Response.End()
        ''#### ORI ####

    End Sub



Code for disable all server side control on the datagrid

VB
Private Sub DisableControls(ByVal gv As Control)
        Dim lb As LinkButton = New LinkButton
        Dim l As Literal = New Literal
        Dim name As String = String.Empty
        Dim i As Integer = 0
        Do While (i < gv.Controls.Count)
            If (gv.Controls(i).GetType Is GetType(LinkButton)) Then
                CType(gv.Controls(i), LinkButton).ForeColor = Color.Black
                l.Text = CType(gv.Controls(i), LinkButton).Text
                gv.Controls.Remove(gv.Controls(i))
                gv.Controls.AddAt(i, l)
            ElseIf (gv.Controls(i).GetType Is GetType(DropDownList)) Then
                l.Text = CType(gv.Controls(i), DropDownList).SelectedItem.Text
                gv.Controls.Remove(gv.Controls(i))
                gv.Controls.AddAt(i, l)
            End If
            If gv.Controls(i).HasControls Then
                DisableControls(gv.Controls(i))
            End If
            i = (i + 1)
        Loop
    End Sub



v1c3r
 
Share this answer
 

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

  Print Answers RSS


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