我创建了一个webform,它将excel文件导入gridview。 现在我想为每个标题添加复选框控件(以及标题文本)。
以下是我所在的代码 的 1。导入Excel数据。 2.将其存储在数据表中 3.动态创建gridview 4.将数据绑定到gridview中。
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
If FileUpload1.HasFile Then
Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
Dim FilePath As String = Server.MapPath(FolderPath + FileName)
FileUpload1.SaveAs(FilePath)
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
End If
End Sub
Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)
Dim conStr As String = ""
Select Case Extension
Case ".xls"
conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
Exit Select
Case ".xlsx"
conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
Exit Select
End Select
conStr = String.Format(conStr, FilePath, isHDR)
Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dt As New DataTable()
cmdExcel.Connection = connExcel
connExcel.Open()
Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
connExcel.Close()
connExcel.Open()
cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
oda.SelectCommand = cmdExcel
oda.Fill(dt)
connExcel.Close()
Dim GridView1 As GridView = New GridView
GridView1.AutoGenerateColumns = False
For i As Integer = 0 To dt.Columns.Count - 1
Dim boundfield As BoundField = New BoundField
boundfield.DataField = dt.Columns(i).ColumnName.ToString()
boundfield.HeaderText = dt.Columns(i).ColumnName.ToString()
GridView1.Columns.Add(boundfield)
Next
GridView1.DataSource = dt
GridView1.DataBind()
Panel1.Controls.Add(GridView1)
End Sub
一切正常。但是当我在标题文本中添加复选框时,我完全是空白的。请建议我可以使用的解决方案或任何其他方法。
答案 0 :(得分:0)
没有评论:(
无论如何,我自己做了。这很简单。我在想太多了。这是代码。
ASPX:
<form id="form1" runat="server">
<div>
Import Excel File:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Has Header ?" />
<asp:RadioButtonList ID="rbHDR" runat="server">
<asp:ListItem Text = "Yes" Value = "Yes" Selected = "True" >
</asp:ListItem>
<asp:ListItem Text = "No" Value = "No"></asp:ListItem>
</asp:RadioButtonList>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
代码隐藏:
Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)
Dim conStr As String = ""
Select Case Extension
Case ".xls"
'Excel 97-03
conStr = ConfigurationManager.ConnectionStrings("Excel03ConString") _
.ConnectionString
Exit Select
Case ".xlsx"
'Excel 07
conStr = ConfigurationManager.ConnectionStrings("Excel07ConString") _
.ConnectionString
Exit Select
End Select
conStr = String.Format(conStr, FilePath, isHDR)
Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dt As New DataTable()
cmdExcel.Connection = connExcel
connExcel.Open()
Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
connExcel.Close()
connExcel.Open()
cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
oda.SelectCommand = cmdExcel
oda.Fill(dt)
connExcel.Close()
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated
' Check the header type
If e.Row.RowType = DataControlRowType.Header Then
Dim litHeader As Literal
Dim txtSearch As CheckBox
' loop through each cell
For i As Integer = 0 To (e.Row.Cells.Count - 1)
litHeader = New Literal
txtSearch = New CheckBox
' get the current header text
litHeader.Text = e.Row.Cells(i).Text & " "
' add the header text plus a new textbox
e.Row.Cells(i).Controls.Add(txtSearch)
e.Row.Cells(i).Controls.Add(litHeader)
Next
End If
End Sub