我有ListBox
填充了SqlDataReader
对象,它看起来很棒,但我遇到的问题是我希望数据文本字段显示来自{{1的数据} date
查询的字段,SQL
的数据值来自ListBox
字段。如果在Visual Studio 2010中使用查询构建器功能,则这很容易实现。只需单击ListBox并更改右侧属性列中的属性即可。但是,由于我没有使用查询构建器功能并且我手动编码,我无法弄清楚如何将列表框的数据文本字段更改为url
字段和数据值字段到date
字段。
这背后的逻辑是,用户可以点击其项目的日期,点击按钮,它将导航到url
中提供的url
。< / p>
以下是我在按钮点击操作中使用的代码;
SQL Database
以下是一些有助于说明的图片。
答案 0 :(得分:0)
internal class LbItem
{
internal string url;
internal string data;
public override string ToString()
{
return this.data;
}
}
并将此类的实例添加到ListBox.Items:
listBox1.Items.Add(new LbItem { url = "http:xyz", data = "123" });
列表框将显示由重写的ToString()函数返回的LbItems ...
答案 1 :(得分:0)
我将代码更改为以下内容,以回答我的问题!
Protected Sub SearchButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SearchButton.Click
Dim startDate As Date
Dim endDate As Date
startDate = TextStartDate.Text
endDate = TextEndDate.Text
Dim connectionString As String
startDate = TextStartDate.Text
endDate = TextEndDate.Text
connectionString = SearchDataSource.ConnectionString.ToString
Dim sqlConnection1 As New SqlConnection(connectionString)
Using sqlConnection1
Dim command As SqlCommand = New SqlCommand( _
"SELECT first_name, last_name, date, url FROM tbl_paystubs WHERE date>='" + startDate.ToString + "' AND date<='" + endDate.ToString + "';", _
sqlConnection1)
sqlConnection1.Open()
Dim da As New SqlDataAdapter(command)
Dim ds As New DataSet()
da.Fill(ds)
sqlConnection1.Close()
SearchListBox.DataSource = ds
SearchListBox.DataTextField = "date"
SearchListBox.DataValueField = "url"
SearchListBox.DataBind()
End Using
TextStartDate.Text = ""
TextEndDate.Text = ""
End Sub