我有一个在gridview之外的下拉列表。下拉列表值来自主表。我想根据下拉列表的选定值来筛选gridview中的数据。请帮助我实现这个目标,因为我是新手,并且之前没有这样做过。 请参阅我的下拉列表代码: -
<asp:DropDownList ID="ddlPagesNgo" runat="server" CssClass="selectpicker form-control-drp wd" Style="width: 100%" AutoPostBack="false"></asp:DropDownList>
Cs代码: -
private void Binddropdownlist()
{
SqlCommand cmd = new SqlCommand("Select * from tbl_ngoname", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlPagesNgo.DataTextField = ds.Tables[0].Columns["ngo_name"].ToString();
ddlPagesNgo.DataSource = ds.Tables[0];
ddlPagesNgo.DataBind();
ddlPagesNgo.Items.Insert(0, new ListItem("--Select NGO--", "0"));
}
另请参阅gridview代码
<asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3"
AutoGenerateColumns="False" OnDataBound="grdCSRPageData_DataBound" AllowPaging="true" CssClass="hoverTable"
OnPageIndexChanging="grdCSRPageData_PageIndexChanging" DataKeyNames="Id" OnRowDeleting="grdCSRPageData_RowDeleting"
PageSize="5" ShowFooter="true" OnRowEditing="grdCSRPageData_RowEditing" OnRowUpdating="grdCSRPageData_RowUpdating"
OnRowCancelingEdit="grdCSRPageData_RowCancelingEdit">
<AlternatingRowStyle CssClass="k-alt" BackColor="#f5f5f5"/>
<Columns>
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%" HeaderStyle-CssClass="k-grid td" >
<ItemTemplate>
<asp:Checkbox ID="chkSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="page_title" HeaderText="Page Title" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:BoundField DataField="Active" HeaderText="Active" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%" HeaderStyle-CssClass="k-grid td" >
<ItemTemplate>
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png" UpdateImageUrl="~/images/update.png">
<ControlStyle Height="20px" Width="20px"></ControlStyle>
</asp:CommandField>
</Columns>
</asp:GridView>
如果您还有其他需要,请告诉我。
获取数据功能代码: -
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
try
{
conn.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
sda.Dispose();
conn.Dispose();
}
}
答案 0 :(得分:1)
在下拉列表的选定索引更改事件中获取选定值,并将此值传递给查询生成数据源以绑定到gridview
protected void ddlPagesNgo_SelectedIndexChanged(object sender, EventArgs e)
{
BindGrid(ddlPagesNgo.SelectedValue);
}
您的下拉列表现在应该是
<asp:DropDownList ID="ddlPagesNgo" runat="server" CssClass="selectpicker form-control-drp wd" Style="width: 100%" AutoPostBack="True" OnSelectedIndexChanged="ddlPagesNgo_SelectedIndexChanged"></asp:DropDownList>
对gridview绑定功能的更改
public void BindGrid(int selectedID)
{
string strQuery = "select Id,page_title,page_description,meta_title,meta_keywords,meta_description,Active from tbl_Pages";
strQuery +=" WHERE Id = "+selectedID;
strQuery +=" ORDER By Id DESC";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
grdCSRPageData.DataSource = dt;
grdCSRPageData.DataBind();
}
答案 1 :(得分:0)
我认为您必须执行以下操作:
答案 2 :(得分:0)
将DropviewSelectedIndex上的Gridview绑定更改。在DropdownSelection检查
protected void ddlgrid_SelectedIndexChanged(object sender, EventArgs e)
{
bindGridview();
if (ddlgrid.SelectedItem.Value == "1") // Dropdownlist Value
{
// Write your Sql Procedure or Query To Display result
// It will return only selected value in Gridview.
}
if (ddlgrid.SelectedItem.Value == "2")
{
// same here
}
}