【发布时间】:2012-09-20 18:28:03
【问题描述】:
我还有一些关于我最新项目的问题。我觉得在过去的几天里我取得了一些相当不错的进步,但我仍然在 SQL 库的一些核心概念上苦苦挣扎,即从特定列读取和删除整行。
在上周,我能够构建一个网络表单,将 excel 文件保存到服务器,打开这些文件并将数据导出到特定的 SQL 表中,并根据用户通过下拉列表选择的内容将数据绑定到特定的数据网格.
我想要完成的是:另一个下拉列表的动态填充取决于用户从第一个下拉列表中选择的内容。更具体地说,我有 4 个表,在每个表的第一列中我有序列号,如果用户在第一个下拉列表中选择 Table2,我希望第二个下拉列表显示 Table2 的 column1 中的所有序列号。然后,如果用户从第二个淹没中选择一个特定的序列号,它会使用该相关行的第 1-5 列填充一个数据网格。
第二部分是创建一个删除按钮,用户可以在数据网格中显示信息后按下该按钮,从该表中删除序列号条目的整行。
这就是我从其他例子中设法将科学怪人放在一起的:
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs)
DropDownList2.Enabled = True 'its remains disabled until the user selects something from the first box
Using con As New SqlClient.SqlConnection
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & AppPath & "App_Data\DeviceDatabase.MDF;Integrated Security=True;User Instance=True;"
Using cmd As New SqlClient.SqlCommand
cmd.Connection = con
End Using
Dim cmdSQL As New SqlCommand()
cmdSQL.CommandType = Data.CommandType.Text
cmdSQL.CommandText = "SELECT Fieldname1 FROM " & """" & DropDownList1.SelectedItem.ToString & """" 'Im pretty sure this isnt right, and the reason I use """"" is because some of the items in the dropdown have spaced words.
Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
Dim myDataSet As New DataSet()
adptSQL.Fill(myDataSet)
With myDataSet.Tables(DropDownList1.SelectedIndex) 'I think this is right
For rowNumber As Integer = 0 To .Rows.Count - 1
With .Rows(rowNumber)
DropDownList2.Items.Add(col1.rowNumber) 'This is obviously not working
End With
Next
End With
End Using
End Sub
然后,我不太确定如何使用所选行填充数据表,尽管目前我可以使用以下方法完成整个表:
Private Sub GenTables(ByVal DropList As Object)
If DropList.SelectedIndex = 0 Then
GridView1.DataSourceID = Nothing
ElseIf DropList.SelectedIndex = 1 Then
GridView1.DataSourceID = "SqlDataSource1"
ElseIf DropList.SelectedIndex = 2 Then
GridView1.DataSourceID = "SqlDataSource2"
ElseIf DropList.SelectedIndex = 3 Then
GridView1.DataSourceID = "SqlDataSource3"
ElseIf DropList.SelectedIndex = 4 Then
GridView1.DataSourceID = "SqlDataSource4"
End If
GridView1.DataBind()
End Sub
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DeviceDatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DeviceDatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT [Device:] AS column1, [SWversion:] AS column2, [Date:] AS column3, [Tester:] AS column4, [Wifi Preferred InCov:] AS column5 FROM [Galaxy Nexus]">
</asp:SqlDataSource>
'there are 3 more of these.
但是我将这些表“硬编码”到应用程序中,显然我不能对每个表行都执行此操作。那么如何在不提前在 asp 中设置 SQLDataSource 的情况下填充数据网格呢?
最后,单击按钮删除与数据网格中显示的信息相关的行。如果第一部分能得到一点帮助,我相信我能弄清楚第二部分。
我要问的几乎是:如何使用来自 Coloumn1 的所有项目填充下拉列表?以及如何从特定行填充数据网格?
我们非常感谢任何和所有的帮助。谢谢各位
扎克
编辑
嗯,我想我让这件事变得更加困难了,现在我正在处理这个问题:
Protected Sub BindDrop_Click(sender As Object, e As System.EventArgs)
DropDownList2.DataSourceID = "SqlDataSource5"
DropDownList2.DataBind()
End Sub
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:DeviceDatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DeviceDatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT [Device:] AS column1 FROM [Galaxy Nexus]">
它不太正确,但它更接近并且在 1/10 行
【问题讨论】:
-
您可以稍微细化您的问题,这可能会帮助您获得更多答案,例如删除前 2 段,因为它们与问题无关。我不太清楚你到底想做什么。
-
简单的问题是:我试图将 dropdown2 与 dropdown1 中任何表的 column1(序列)绑定。然后在用户在 dropdown2 中选择一个序列后,它会使用包含 table(dropdown1) 中的序列的行和行(dropdown2) 更新数据网格
标签: asp.net vb.net visual-studio-2010 sql-server-2008