无法理解如何将数据表数据传输到ListView
本文关键字:数据表 数据传输 ListView | 更新日期: 2023-09-27 18:11:42
我试图显示从存储过程到ListView的输出(以多个数据表的形式)。我试图遵循从c#数据表到listview的答案,但我传递的参数不是正确的类型。
我的ASPX文件包含以下内容:
<asp:WizardStep runat="server">
<asp:SqlDataSource ID="GetHighSchoolTeamInfo" runat="server" ConnectionString="<%$ ConnectionStrings:FollowingHSFootballConnectionString %>"
SelectCommand="Select [HighSchoolFootballTeam$].[HighSchoolName], [HighSchoolFootballTeam$].[HighSchoolTeamID] from [HighSchoolFootballTeam$]
Where [HighSchoolFootballTeam$].[HighSchoolName] = @HighSchoolName">
<SelectParameters>
<asp:ControlParameter Name="HighSchoolName" ControlID="CheckBoxClassRegion" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:button id="Submit1" runat="server" Text="Submit" OnClick="CheckBoxClassRegion_btnSubmit" />
<asp:Label ID="AddressText" runat="server" />
</asp:WizardStep>
:
:
:
<asp:ListView ID="ListView1" runat="server">
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Label Text='<%# Eval("Date") %>' runat="server" ID="DateLabel" /></td>
<td>
<asp:Label Text='<%# Eval("HighSchoolName") %>' runat="server" ID="HighSchoolNameLabel" /></td>
<td>
<asp:Label Text='<%# Eval("HighSchoolName1") %>' runat="server" ID="HighSchoolName1Label" /></td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" />
<asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" />
</td>
<td>
<asp:TextBox Text='<%# Bind("Date") %>' runat="server" ID="DateTextBox" /></td>
<td>
<asp:TextBox Text='<%# Bind("HighSchoolName") %>' runat="server" ID="HighSchoolNameTextBox" /></td>
<td>
<asp:TextBox Text='<%# Bind("HighSchoolName1") %>' runat="server" ID="HighSchoolName1TextBox" /></td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" />
<asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" />
</td>
<td>
<asp:TextBox Text='<%# Bind("Date") %>' runat="server" ID="DateTextBox" /></td>
<td>
<asp:TextBox Text='<%# Bind("HighSchoolName") %>' runat="server" ID="HighSchoolNameTextBox" /></td>
<td>
<asp:TextBox Text='<%# Bind("HighSchoolName1") %>' runat="server" ID="HighSchoolName1TextBox" /></td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label Text='<%# Eval("Date") %>' runat="server" ID="DateLabel" /></td>
<td>
<asp:Label Text='<%# Eval("HighSchoolName") %>' runat="server" ID="HighSchoolNameLabel" /></td>
<td>
<asp:Label Text='<%# Eval("HighSchoolName1") %>' runat="server" ID="HighSchoolName1Label" /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table runat="server" id="itemPlaceholderContainer" style="" border="0">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">Date</th>
<th id="Th2" runat="server">HighSchoolName</th>
<th id="Th3" runat="server">HighSchoolName1</th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Label Text='<%# Eval("Date") %>' runat="server" ID="DateLabel" /></td>
<td>
<asp:Label Text='<%# Eval("HighSchoolName") %>' runat="server" ID="HighSchoolNameLabel" /></td>
<td>
<asp:Label Text='<%# Eval("HighSchoolName1") %>' runat="server" ID="HighSchoolName1Label" /></td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
文件后面的c#代码包含以下
> protected void CheckBoxClassRegion_btnSubmit(object sender, EventArgs e)
> {
> int i;
> AddressText.Text += " <br />";
> /**********************************************************************/
> /* The code below will initialize the connection to the database. */
> /* As the connection string to the SQL database is defined as conn, */
> /* the open method from conn will connect to the database, and the */
> /* cmd variable will call on the stored procedure GetSchedule. */
> /**********************************************************************/
> string strcon = WebConfigurationManager.ConnectionStrings["FollowingHSFootballConnectionString"].ConnectionString;
> using (SqlConnection conn = new SqlConnection(strcon))
> {
> SqlCommand cmd = new SqlCommand("GetSchedule", conn);
> cmd.CommandType = CommandType.StoredProcedure;
> conn.Open();
> /**********************************************************************/
> /* The for loop below will determine which items from the checkbox */
> /* were selected from the input and use the High School team name to */
> /* pass to the stored procedure 'GetSchedule' to return the dates, */
> /* home team and away team each game. */
> /**********************************************************************/
> /******************************************************************************************************************************************/
> foreach (ListItem item in CheckBoxClassRegion.Items) /******************************************************************/
> { /* This loop will go through all of the checkboxed items */
> if (item.Selected == true) /******************************************************************/
> { /* If this team has been selected */
> cmd.Parameters.Clear(); /* Pass input parameter "Team Name" */
> cmd.Parameters.AddWithValue("@TeamName", item.Value);
> /******************************************************************/
> SqlDataAdapter da = new SqlDataAdapter(cmd);
> DataTable dt = new DataTable();
> da.Fill(dt);
> for (i = 0; i <= dt.Rows.Count - 1; i++)
> {
> ListViewItem itm = new ListViewItem(dt.Rows[i].ToString());
>
> ListView1.DataSource.ToString(itm);
>
> }
>
> }
> }
> }
> }
当da.Fill(dt);,所以我的困惑是后面的循环。谁能帮助我理解什么数据类型或可能是什么方法是正确的方法?
谢谢
就像我之前说的,你正试图使用一个Windows窗体ListView的例子,它的工作方式不一样。
幸运的是,这样做要容易得多。首先,创建一个类来表示查询元组。我不知道类型是否与我使用的相同,如果需要,只需更新它:
// Create a class to store every tuple of your query.
public class QueryTuple
{
public DateTime Date { get; set; }
public string HighSchoolName { get; set; }
public string HighSchoolName1 { get; set; }
// For NULL values that maps to struct types, use Nullable<T>
public Nullable<int> AwayScore {get; set; }
// Nullable types can also be written with a ? after the type
public int? HomeScore { get; set; }
}
然后,像这样更新你的foreach代码。
List<QueryTuple> tuples = new List<QueryTuple>();
foreach (ListItem item in CheckBoxClassRegion.Items)
{
if (item.Selected == true)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@TeamName", item.Value);
// Make sure your connection is open before ExecuteReader
using (var dr = cmd.ExecuteReader())
{
while (dr.Read())
{
QueryTuple tuple = new QueryTuple
{
Date = (DateTime)dr["Date"],
HighSchoolName = (string)dr["HighSchoolName"],
HighSchoolName1 = (string)dr["HighSchoolName1"],
// Mapping nullable types may need a bit more coding
HomeScore = dr["HomeScore"] == DBNull.Value ? new int?() : (int)dr["HomeScore"],
AwayScore = dr["AwayScore"] == DBNull.Value ? new int?() : (int)dr["AwayScore"]
};
tuples.Add(tuple);
}
}
}
}
// Finally, you set the DataSource property and call the DataBind method
ListView1.DataSource = tuples;
ListView1.DataBind();