0

我有从 datagridview 导出到 Excel 的功能,我通过按 Tab 键来尝试点击界面上的每个按钮来进行测试,以将指示器定位到每个按钮。但是在导出按钮上时。它给了我一个错误

DataGrid with id '' could not automatically generate any columns from the selected data source.

但是当我用指针/鼠标单击它时,它工作正常。

这是我导出到 excel 代码:

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click

        Dim dt As New DataTable
        If CInquiry.SearchInquiry(txtAccount.Text, txtCustName.Text, txtAmount.Text, dropResponse.SelectedValue.ToString, txtInquiryDate.Text) Then
            dt = CInquiry.DT
        Else
            eMessage(CInquiry.eMsg)
        End If
        Dim DataGrd As New DataGrid()
        DataGrd.DataSource = dt.DefaultView
        DataGrd.DataBind()

        Dim attachment As String
        attachment = "attachment; filename=Inquiry_Report" & Format(Now, "ddMMMyyyy") & ".xls"
        Response.Buffer = True
        Response.ClearContent()
        Response.ClearHeaders()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/ms-excel"
        Dim sw As New StringWriter()
        Dim htw As New HtmlTextWriter(sw)
        DataGrd.RenderControl(htw)
        Response.Write(sw.ToString())
        Response.End()
    End Sub

错误是当数据网格尝试绑定数据时,只有当我按照上面所说的操作时才会出现错误。怎么了?它是 onclick 它的意思是仅通过指针/鼠标单击吗?

4

1 回答 1

0

嗨,实际上我现在没有 VB 代码,但我可以发布可能对你有帮助的 c# 代码

 protected void lnkfiledownload_Click(object sender, EventArgs e)
    {

        string attachment = "attachment; filename=notification.xls";

        Response.ClearContent();

        Response.AddHeader("content-disposition", attachment);

        Response.ContentType = "application/ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        // Create a form to contain the grid

        HtmlForm frm = new HtmlForm();

        gvBranchNotification.Parent.Controls.Add(frm);

        frm.Attributes["runat"] = "server";

        frm.Controls.Add(gvBranchNotification);

        frm.RenderControl(htw);



        Response.Write(sw.ToString());

        Response.End();

    }
于 2013-05-24T10:01:56.083 回答