我有功能从datagridview导出到Excel,我通过按下选项卡尝试点击界面上的每个按钮,找到每个按钮的指示器。但是当出口按钮时。它给我一个错误
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
错误是当datagrid尝试绑定数据时,只有当我按上述方式执行操作时才会引发错误。怎么了?它只是通过指针/鼠标点击它的平均点击吗?
答案 0 :(得分: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();
}