过滤GridView on按钮单击Dropdownlist和Textboxes

时间:2015-07-04 04:51:44

标签: c# asp.net gridview

我需要的是。我希望在点击按钮时过滤并显示GridView中的记录,具体取决于我想要的数据。

对于EX: - 我有dropdownlistTextbox,我将在其中选择我想要过滤的数据,然后点击按钮,相关数据将会显示在Gridview中。

请注意: - DropdownlistTextboxesButton所有外部 Gridview。

我尝试了Here的代码,但实际上并不是我想要的。看看下面的代码。

ASPX

<div>
    <table>
        <tr>
            <td>
                <asp:Label ID="lblProject" runat="server" Text="Project Name">Project Name</asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="ddlProjectName" runat="server" AutoPostBack="true">
                </asp:DropDownList>
            </td>
            <td>
                <asp:Label ID="lblFrmDate" runat="server" Text="From Date">From Date</asp:Label>
            </td>
            <td>
                <input id="TxtIndate" runat="server" maxlength="20" type="text" style="text-align: right;
                    width: 75px" readonly="readonly" />
                <cc3:Calendar ID="Calendar2" runat="server" CultureName="en-GB" DatePickerImagePath="Images/icon2.gif"
                    DatePickerMode="true" TextBoxId="TxtIndate">
                </cc3:Calendar>
            </td>
            <td>
                <asp:Label ID="lblToDate" runat="server" Text="To Date">To Date</asp:Label>
            </td>
            <td>
                <input id="txtOutDate" runat="server" maxlength="20" type="text" style="text-align: right;
                    width: 75px" readonly="readonly" />
                <cc3:Calendar ID="Calendar1" runat="server" CultureName="en-GB" DatePickerImagePath="Images/icon2.gif"
                    DatePickerMode="true" TextBoxId="txtOutDate">
                </cc3:Calendar>
            </td>
            <td>
                <asp:Button ID="btnSubmit" runat="server" Width="100" Text="Search" />
            </td>
        </tr>
    </table>
</div>
<br />
<br />
<div>
    <asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
    </asp:GridView>
</div>

另请参阅 CS代码: -

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        fillProject();
    }
    grdData.Visible = false;
}
private void fillProject()
{
   // dt.Clear();
    ObjPriDaAdp = new OracleDataAdapter("select distinct Project_id, PROPERTY_NAME Project_name  from xxcus.XXACL_PN_PROJBUILD_V order by PROPERTY_NAME", ObjPriCon);
    ObjPriDaAdp.Fill(dt);
    ddlProjectName.DataTextField = "Project_name";
    ddlProjectName.DataValueField = "Project_id";
    ddlProjectName.DataSource = dt;
    ddlProjectName.DataBind();
    ddlProjectName.Items.Insert(0, new ListItem("--- Select ---", "0"));
}

请建议如何处理这些情况。可能是我无法从这些开始: -

另外,我想在gridview中显示DateProjectNameVehicleNO TimeInTimeOut

2 个答案:

答案 0 :(得分:1)

IN CS Page

<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Vertical">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
        <asp:BoundField HeaderText="PROPERTY NAME" DataField="PROPERTY_NAME" SortExpression="PROPERTY_NAME">
            <HeaderStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Vehicle No" DataField="Vehicle_No" SortExpression="Vehicle_No">
            <HeaderStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Creation date" DataField="Creation_date" SortExpression="Creation_date">
            <HeaderStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Time In" DataField="Time_In" SortExpression="Time_In">
            <HeaderStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Time Out" DataField="Time_Out" SortExpression="Time_Out">
            <HeaderStyle HorizontalAlign="Center" />
        </asp:BoundField>
    </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>

和ASPX页面

 public virtual List<Student> Students { get; set; }  

答案 1 :(得分:0)

在你的按钮上&#34; btnSubmit&#34; click事件保存textbox和dropdownlist的值,然后将该值传递给sql查询,如:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //connection
            string query = "select * from tablename where Projectname like "+ddlProjectName.SelectedValue+"%";// this is an sample query change as per your requirement
             SqlDataAdapter da = new SqlDataAdapter(query,connection);
             DataTable dt = new DataTable();
             da.Fill(dt);
             grdData.DataSource =dt;
             grdData.DataBind(); 
        }

这里有一个链接可能有助于更多: http://www.c-sharpcorner.com/UploadFile/0c1bb2/searching-records-from-database-and-display-in-gridview-usin/