我想过滤网格视图。我使用存储过程来绑定数据。
这是我的代码:
<asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="search"/>
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" EditRowStyle-Width="50px" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="TaskGridView_RowCancelingEdit" AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10" >
<Columns>
<asp:TemplateField HeaderStyle-Width="30px" ItemStyle-Width="30px" >
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Uusername" ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px" HeaderStyle-Width="130px" ItemStyle-Width="130px" />
<asp:BoundField DataField="Umail" ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px" HeaderStyle-Width="160px" ItemStyle-Width="160px" />
<asp:BoundField DataField="Pname" ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px" HeaderStyle-Width="100px" ItemStyle-Width="100px" />
<asp:BoundField DataField="Pfamily" ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px" HeaderStyle-Width="150px" ItemStyle-Width="150px" />
<asp:BoundField DataField="UisActiveMob" ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px" HeaderStyle-Width="100px" ItemStyle-Width="100px" />
</Columns>
</asp:GridView>
我像这样填写gridview:
con.Open();
string query = "getDataProfile";
SqlCommand com = new SqlCommand(query, con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(com);
DataSet dset = new DataSet();
adapter.Fill(dset, "t1");
var result = com.ExecuteReader();
GridView1.EmptyDataText = "No Records Found";
GridView1.DataSource = dset.Tables["t1"];
GridView1.DataBind();
con.Close();
我知道如果我使用数据源,我必须添加这样的过滤器:
<FilterParameters>
<asp:ControlParameter ControlID="CountryListBox" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="LastNameTextBox" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
但我没有使用数据源!如何在gridview中搜索?
答案 0 :(得分:2)
您应该更改可以获取参数的存储过程代码。假设您的存储过程是:
//Add @Country and @LastName Parameters to stored procedure.
CREATE PROCEDURE getDataProfile (@Country INT, @LastName NVarchar(50))
As
//Your stored procedure code goes here
注意:您不需要任何SqlDataSource
。更改来源部分:
<asp:Button ID="btnSearch" runat="server" Text="search"/>
要:
<asp:Button ID="btnSearch" OnClick="btnSearch_Click" runat="server" Text="search"/>
还要注意将连接字符串更改为连接字符串。 您的代码必须如下:
protected void btnSearch_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Your Connection String";
con.Open();
string query = "getDataProfile";
SqlCommand com = new SqlCommand(query, con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter param = com.CreateParameter();
param.ParameterName = "@Country";
param.Value = CountryListBox.SelectedValue;
param.DbType = DbType.Int32;
com.Parameters.Add(param);
param = com.CreateParameter();
param.ParameterName = "@LastName";
param.Value = LastNameTextBox.Text;
param.DbType = DbType.String;
com.Parameters.Add(param);
SqlDataAdapter adapter = new SqlDataAdapter(com);
DataSet dset = new DataSet();
adapter.Fill(dset, "t1");
var result = com.ExecuteReader();
GridView1.EmptyDataText = "No Records Found";
GridView1.DataSource = dset.Tables["t1"];
GridView1.DataBind();
con.Close();
}