Я привязываю GridView к sqldatasource, а затем на _rowcreated event, выполняющем некоторые проверки, и когда строка не соответствует требованиям, я скрываю ее с помощью e.Row.Visible = false;export GridView to excel # 2
Это прекрасно работает и отображает только правильные строки в gridview. Теперь у меня есть кнопка для экспорта в excel, и это отлично работает, за исключением экспорта скрытых строк. Я не хочу, чтобы скрытые строки экспортировались.
Есть ли способ, которым я могу сказать, что gridview не добавляет эту строку, а не скрывает ее? Есть ли простой способ удалить все скрытые строки до запуска экспорта? Могу ли я не добавлять скрытые строки во время экспорта? Как вы можете видеть в приведенном ниже коде, я попытался сделать это, но он не распознает, является ли строка видимой или нет.
Экспорт Код:
public static void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
gv.GridLines = GridLines.Both;
table.GridLines = gv.GridLines;
//table.BackColor = Color.Yellow;
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
//color the header
table.Rows[0].BackColor = gv.HeaderStyle.BackColor;
table.Rows[0].ForeColor = gv.HeaderStyle.ForeColor;
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
if (row.Visible == true)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
}
// color the rows
bool altColor = false;
for (int i = 1; i < table.Rows.Count; i++)
{
if (!altColor)
{
table.Rows[i].BackColor = gv.RowStyle.BackColor;
altColor = true;
}
else
{
table.Rows[i].BackColor = gv.AlternatingRowStyle.BackColor;
altColor = false;
}
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
Почему у вас есть скрытые строки в первую очередь? только получить данные, которые вы хотите отобразить. – Joakim