Ok, here is what I really what to do. How to I hide the DistributorID when loaded but display it when the excel file is exported.
You might be thinking why don't I delete it form the page_load then, well I can't do that cause then my cells will not match up to the database
to the LinkButton1_Click event
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class manager_view_distributors : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AlternaDB.MarketingDataContext db = new AlternaDB.MarketingDataContext();
var products = from p in db.Distributors
select new { p.DistributorID, Company_Name = p.CompanyName, Address = p.AddressLine1, p.City, Zip_Code = p.ZipCode, p.State, First_Name = p.Contact1FirstName, Last_Name = p.Contact1LastName, Area_Code = p.Contact1WorkAreaCode, Phone = p.Contact1WorkNumber, Num_Stores = p.NumStores, Num_DSC = p.NumDsc };
GridView1.DataSource = products;
GridView1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageSize = Int32.Parse(DropDownList1.SelectedValue); this.GetData();
}
private void GetData()
{
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.GetData();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (this.rdoBtnListExportOptions.SelectedIndex == 1)
{
// the user wants all rows exported, turn off paging
// and rebing the grid before sending it to the export
// utility
ExportExcelColumns();
this.GridView1.PageSize = 1000000;
GridView1.DataBind();
}
else if (this.rdoBtnListExportOptions.SelectedIndex == 2)
{
// the user wants just the first 100,
// adjust the PageSize and rebind
ExportExcelColumns();
this.GridView1.PageSize = 100;
GridView1.DataBind();
}
//Current Page export
else if (this.rdoBtnListExportOptions.SelectedIndex == 0)
{
ExportExcelColumns();
GridView1.DataBind();
}
GridViewExportUtil.Export("Customers.xls", this.GridView1);
}
private void ExportExcelColumns()
{
// pass the grid that for exporting ...
AlternaDB.MarketingDataContext db = new AlternaDB.MarketingDataContext();
var dist = from d in db.Distributors
select new
{
d.DistributorID,
AccountType = SalonForm.DistAccountType.GetDistAccountTypeByValue((int)d.AccountType),
AccountStatus = SalonForm.AccountStatus.GetAccountStatusByValue((int)d.AccountStatus),
d.CompanyName,
d.AddressLine1,
d.AddressLine2,
d.City,
d.State,
d.Country,
d.ZipCode,
d.Territory,
d.NumDsc,
d.NumStores,
d.Website,
Contact1Type = SalonForm.DistCustomerType.GetDistCustomerTypeByValue((int?)d.Contact1Type),
d.Contact1FirstName,
d.Contact1LastName,
d.Contact1Email,
d.Contact1WorkIntlPrefix,
d.Contact1WorkAreaCode,
d.Contact1WorkNumber,
d.Contact1WorkExtension,
d.Contact1CellIntlPrefix,
d.Contact1CellAreaCode,
d.Contact1CellNumber,
d.Contact1CellExtension,
d.Contact1ReceiveEnews,
Contact2Type = SalonForm.DistCustomerType.GetDistCustomerTypeByValue((int?)d.Contact2Type),
d.Contact2FirstName,
d.Contact2LastName,
d.Contact2Email,
d.Contact2WorkIntlPrefix,
d.Contact2WorkAreaCode,
d.Contact2WorkNumber,
d.Contact2WorkExtension,
d.Contact2CellIntlPrefix,
d.Contact2CellAreaCode,
d.Contact2CellNumber,
d.Contact2CellExtension,
d.Contact2ReceiveEnews,
Contact3Type = SalonForm.DistCustomerType.GetDistCustomerTypeByValue((int?)d.Contact3Type),
d.Contact3FirstName,
d.Contact3LastName,
d.Contact3Email,
d.Contact3WorkIntlPrefix,
d.Contact3WorkAreaCode,
d.Contact3WorkNumber,
d.Contact3WorkExtension,
d.Contact3CellIntlPrefix,
d.Contact3CellAreaCode,
d.Contact3CellNumber,
d.Contact3CellExtension,
d.Contact3ReceiveEnews,
Contact4Type = SalonForm.DistCustomerType.GetDistCustomerTypeByValue((int?)d.Contact4Type),
d.Contact4Firstname,
d.Contact4LastName,
d.Contact4Email,
d.Contact4WorkIntlPrefix,
d.Contact4WorkIntlAreaCode,
d.Contact4WorkNumber,
d.Contact4WorkExtension,
d.Contact4CellIntlPrefix,
d.Contact4CellAreaCode,
d.Contact4CellNumber,
d.Contact4CellExtension,
d.Contact4ReceiveEnews,
Contact5Type = SalonForm.DistCustomerType.GetDistCustomerTypeByValue((int?)d.Contact5Type),
d.Contact5FirstName,
d.Contact5LastName,
d.Contact5Email,
d.Contact5WorkIntlPrefix,
d.Contact5WorkAreaCode,
d.Contact5WorkNumber,
d.Contact5WorkExtension,
d.Contact5CellIntlPrefix,
d.Contact5CellAreaCode,
d.Contact5CellNumber,
d.Contact5CellExtenstion,
d.Contact5ReceiveEnews,
d.DateCreated,
d.Username,
d.Password
};
GridView1.DataSource = dist;
}
protected void BrowseDistGrid_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void rdoBtnListExportOptions_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void LinkButton1_Click1(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((LinkButton)sender).Parent.Parent; //get that selected row
//string AccountType = gvr.Cells[16].Text;
string CompanyName = gvr.Cells[2].Text;
string Address1 = gvr.Cells[3].Text;
Response.Redirect("Distributor.aspx?CompanyName=" + CompanyName
+ "&Address1=" + Address1
); //redirect to next page with selected values
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header | e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Controls[1].Visible = false;
}
}
}
Mahesh ChandPosted Nov 23, 2008, 11:59 AM
You don't need to delete it. There are two ways to do it. First, you can hide that column from the GridView. Second, you create another DataSet and use Copy command to copy current DataSet to this new once and then Delete the DistributorID column and use this DataSet to bind on the UI and use old DataSet to export. This is how you would do when I was using a DataSet.
Looks like you are using a List. So delete that item from the list.