当前位置 : 主页 > 网络编程 > ASP >

asp.net – 如何使用gridview绑定数据集

来源:互联网 收集:自由互联 发布时间:2021-06-24
这是我的代码.aspx.cs页面 public string makequery(){ string query = string.Empty; if (ddlPortal2.SelectedValue == "Select" tbFrom.Text == null tbTo.Text == null ddlQuery.SelectedValue == "Select") { query = "SELECT * FROM Form3 and O
这是我的代码.aspx.cs页面

public string makequery()
{
    string query = string.Empty;
    if (ddlPortal2.SelectedValue == "Select" && tbFrom.Text == null && tbTo.Text == null && ddlQuery.SelectedValue == "Select")
    {
        query = "SELECT * FROM Form3 and ORDER BY CONVERT(VARCHAR(25), [Datetime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "' DESC";
        return query;
    }
    else if(ddlPortal2.SelectedItem.Text!="Select" && tbFrom.Text!=null && tbTo.Text!=null && ddlQuery.SelectedItem.Text=="Select")
    {
        query = "Select * from Form3 where Portal='" + ddlPortal2.SelectedValue + "' and ORDER BY CONVERT(VARCHAR(25), [Datetime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "' DESC";
        return query;
    }
    else if (ddlPortal2.SelectedItem.Text != "Select" && tbFrom.Text == null && tbTo.Text == null && ddlQuery.SelectedItem.Text != "Select")
    {
        query = "Select * from Form3 where Portal='" + ddlPortal2.SelectedValue + "' and ORDER BY CONVERT(VARCHAR(25), [Datetime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "' DESC";
        return query;
    }
    return null;
}
public void GridViewBind()
{
    string query = makequery();
    DataSet ds = new DataSet();
    if (ds != null)
    {
        if (ds.Tables[0].Rows.Count != 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
protected void btnSearch_Click(object sender, EventArgs e)
{
    this.GridViewBind();
}

}
.aspx页面代码:

<div id="Portal" runat="server">
<asp:Label ID="lblPortal" runat="server" Text="Select Portal"/>
    <asp:DropDownList ID="ddlPortal2" runat="server" AutoPostBack="True">
        <asp:ListItem>Select</asp:ListItem>
        <asp:ListItem>TRAVELONG</asp:ListItem>
        <asp:ListItem>ONETRAVEL</asp:ListItem>
        <asp:ListItem>.UK-BSP</asp:ListItem>
        <asp:ListItem>.CV-YYZ</asp:ListItem>
        <asp:ListItem>.CV-YVR</asp:ListItem>
    </asp:DropDownList>
   <asp:Label ID="lbFrom" Text="From" runat="server" />
<asp:TextBox ID="tbFrom" runat="server" />
 <asp:RequiredFieldValidator ID="rfvFrom" runat="server" ControlToValidate="tbFrom" ErrorMessage="Enter Valid Date">*</asp:RequiredFieldValidator>
       <asp:Label ID="lblto" Text="To" runat="server" />
<asp:TextBox ID="tbTo" runat="server" />
        <asp:RequiredFieldValidator ID="rfvTo" runat="server" ControlToValidate="tbTo" 
        ErrorMessage="Enter To date">*</asp:RequiredFieldValidator>
         <asp:DropDownList ID="ddlQuery" runat="server" Width="87px">
           <asp:ListItem>All</asp:ListItem>
           <asp:ListItem Value="Query ">Query</asp:ListItem>
           <asp:ListItem>Non Query</asp:ListItem>
    </asp:DropDownList>
<asp:Button ID="btnSearch" runat="server" Text="Search" 
    onclick="btnSearch_Click" ValidationGroup="1"/><br />

        <br />           
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
    </div>
<div id="Main" runat="server">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" onrowcommand="GridView1_RowCommand" 
        AutoGenerateColumns="False">
        <Columns>
       <asp:TemplateField HeaderText="Select">
            <itemtemplate>
            <asp:LinkButton CommandName="cmdBind"  runat="server" Text='<%#Eval("ID")%>' ID="ID" ToolTip='<%#Eval("ID")%>'>LinkButton                </asp:LinkButton>                                                                   
            </itemtemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                    <asp:Label ID="lblDateTime" runat="server" Text='<%# Eval("DateTime","{0:d}") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>   
            <asp:BoundField DataField="Portal" HeaderText="Portal" SortExpression="Portal" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="TID" HeaderText="TID" SortExpression="TID" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="PNR" HeaderText="PNR" SortExpression="PNR" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="TicketNumber" HeaderText="TicketNumber" SortExpression="TicketNumber" ItemStyle-HorizontalAlign="Center"/>
          <asp:TemplateField HeaderText="Unused Tkt Amount" SortExpression="ddlUnusedAmount" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblUnusedAmount" runat="server" Text='<%#Eval("UnusedTicketAmount")+ "-" + Eval("ddlUnusedAmount")%>' ></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Penality" SortExpression="ddlAirlinePenality" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="lblAirlinePenality" runat="server" Text='<%#Eval("AirlinePenality")+ "-" + Eval("ddlAirlinePenality")%>' ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Net Refund" SortExpression="ddlNetRefundProcess" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="lblNetRefund" runat="server" Text='<%#Eval("NetRefundProcess")+ "-" + Eval("ddlNetRefundProcess")%>' ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Refundable Commission"   SortExpression="ddlRefundableCommission" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="lblRefundableCommission" runat="server" Text='<%#Eval("RefundableCommission")+ "-" + Eval("ddlRefundableCommission")%>' ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
            <asp:BoundField DataField="ESACCode" HeaderText="ESACCode" SortExpression="ESACCode" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="WaiverCode" HeaderText="WaiverCode" SortExpression="WaiverCode" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="RefundType" HeaderText="RefundType" SortExpression="RefundType" ItemStyle-HorizontalAlign="Center" />
             <asp:BoundField DataField="Comment" HeaderText="Bart Comment" ItemStyle-HorizontalAlign="Center"/> 
        <asp:BoundField DataField="Query" HeaderText="ARC Verified" ItemStyle-HorizontalAlign="Center" />
    </Columns>

现在我在这里创建两个函数来进行查询,然后在绑定数据集时使用gridview将该查询与数据集绑定在一起找不到表0.

我想要的是当我选择字段并按下搜索按钮时,我可以根据所选字段查看网格中的详细信息,但我无法将其与我的数据集绑定.你能帮助我吗?

只需对您的代码进行小的更改,如下所示

public string makequery()
{
    string query = string.Empty;
    if (ddlPortal2.SelectedValue == "Select" && tbFrom.Text == null && tbTo.Text == null && ddlQuery.SelectedValue == "Select")
    {
        query = "SELECT * FROM Form3 and ORDER BY CONVERT(VARCHAR(25), [Datetime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "' DESC";
        return query;
    }
    else if(ddlPortal2.SelectedItem.Text!="Select" && tbFrom.Text!=null && tbTo.Text!=null && ddlQuery.SelectedItem.Text=="Select")
    {
        query = "Select * from Form3 where Portal='" + ddlPortal2.SelectedValue + "' and ORDER BY CONVERT(VARCHAR(25), [Datetime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "' DESC";
        return query;
    }
    else if (ddlPortal2.SelectedItem.Text != "Select" && tbFrom.Text == null && tbTo.Text == null && ddlQuery.SelectedItem.Text != "Select")
    {
        query = "Select * from Form3 where Portal='" + ddlPortal2.SelectedValue + "' and ORDER BY CONVERT(VARCHAR(25), [Datetime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "' DESC";
        return query;
    }
    return null;
}
public void GridViewBind()
{
    string query = makequery();
    sqlconnection con=new sqlconnection("Your Connection string"); 

    DataSet ds = new DataSet();

   SqlDataAdapter ad= new SqlDataAdapter(query,con);

   ad.fill(ds);

    if (ds != null)
    {
        if (ds.Tables[0].Rows.Count != 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
protected void btnSearch_Click(object sender, EventArgs e)
{
    this.GridViewBind();
}
网友评论