【发布时间】:2020-09-12 13:28:56
【问题描述】:
我有数据网格,可以在其中显示包含电影的数据库。 我制作了文本框,我想在写作时过滤这个数据网格 这是我的活动代码
protected void wyszukaj_txt_TextChanged(object sender, EventArgs e)
{
string qry = "select tytul, rok_produkcji, gatunek, cena from filmy where tytul like'" + szukaj.Text + "'";
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(qry, con);
DataSet ds = new DataSet();
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
这是数据网格的代码
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Vertical" Height="356px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" RowHeaderColumn="cena" style="z-index: 1; left: 154px; top: 519px; position: absolute; height: 356px; width: 1120px; margin-right: 0px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="id_filmu" HeaderText="ID" ReadOnly="True" SortExpression="id_filmu" Visible="False" />
<asp:BoundField DataField="tytul" HeaderText="Tytuł" SortExpression="tytul" />
<asp:BoundField DataField="gatunek" HeaderText="Gatunek" SortExpression="gatunek" />
<asp:BoundField DataField="rok_produkcji" HeaderText="Rok produkcji" SortExpression="rok_produkcji" />
<asp:BoundField DataField="cena" HeaderText="Cena" SortExpression="cena" />
<asp:TemplateField HeaderText="Kup">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Kup" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" Text="Kup" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
当我尝试按 Enter 键时出现异常。附加信息:元素 DataSource 和 DataSourceID 都在“GridView1”中定义。 我知道我有这个数据源的重复,但我不知道如何更改它。我试图将“DataSourceID”放在现在只有(.cs 文件)DataSource 的地方,但是我对类型(数据集)有错误 有人帮忙吗? 非常感谢!
【问题讨论】:
-
您应该使用
SqlDataSource或设置DataSource,但不能同时使用。如果您想手动绑定,只需从 gridview 标记中删除它:DataSourceID="SqlDataSource1" -
使用内联SQL语句时要小心SQL注入攻击!