【问题标题】:Extract Selected Row from GridView to Excel Sheet从 GridView 中提取选定行到 Excel 工作表
【发布时间】:2016-05-03 08:00:25
【问题描述】:

我有以下网格视图,我想将列添加为图像图标以将所选行提取到 Excel 工作表。图片 onclick 事件背后的代码是什么。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="SuppliersPage.aspx.cs" Inherits="WebApp_UNICEF.SuppliersPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">


<asp:GridView ID="grdMain" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" 
    AutoGenerateSelectButton="True" 
    onselectedindexchanged="grdMain_SelectedIndexChanged" 
    BorderColor="#1EA4DA" BorderStyle="Solid" BorderWidth="1px" 
    CellSpacing="2" onrowdatabound="grdMain_RowDataBound" AllowPaging="True" 
    AllowSorting="True" onpageindexchanging="grdMain_PageIndexChanging" 
    onrowcreated="grdMain_RowCreated" 
    >
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

    <EditRowStyle BackColor="#999999" BorderColor="#19A5D9" BorderStyle="Solid" 
        BorderWidth="1px" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" BorderStyle="Solid" 
        BorderWidth="1px" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

【问题讨论】:

标签: c# asp.net excel gridview


【解决方案1】:

您可以使用ButtonField Column 来显示图像并具有点击功能。

  1. GridView 的Columns 集合中添加&lt;asp:ButtonField&gt; 类型的列。

  2. 在Step1中定义的ButtonFieldImageUrl属性中定义图片的url。

  3. 为 Step1 中定义的 ButtonField 定义 CommandName,任何名称都可以,例如导出到Excel

    1. 在GridView的RowCommand事件中处理Exporting the row to excel的代码。

void grdMain_RowCommand(Object sender, GridViewCommandEventArgs e) {

// CommandName property to determine which button was clicked.
if(e.CommandName=="ExportToExcel")
{
  // Convert the row index stored in the CommandArgument
  // property to an Integer.
  int index = Convert.ToInt32(e.CommandArgument);

// Rest of your code goes here to export to excel

}

//
// MarkUpCode
//
 <asp:ButtonField  CommandName="ExportToExcel" DataTextField="string"  DataTextFormatString="string" HeaderText="string" ImageUrl="uriOfImage" ShowHeader="True|False" SortExpression="string" Text = "Export"> </asp:ButtonField>

【讨论】:

  • 谢谢你,但是当我添加新列时,标题不会改变你能帮我吗,因为它 AutoGenerateColumn 是真的
  • 另外,由于我找不到任何相关的东西,你能帮我导出excel的其余代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多