3

这是我的代码 .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>       

现在,我在这里创建了两个函数,一个是进行查询,第二个是在我绑定时dataset绑定该查询找不到表 0。 gridviewdataset

我想要的是当我选择字段并按下搜索按钮时,我可以根据所选字段在我的网格中查看详细信息,但我无法将它与我的dataset. 你们能帮我解决这个问题吗?

4

6 回答 6

5

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

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();
}
于 2013-04-05T09:07:11.447 回答
2

你需要一些像这样的额外代码

private DataSet GetDataset(string query) 
{
    DataSet dataset = new DataSet();
    using (SqlConnection connection = 
        new SqlConnection(connectionDetailsGoHere))
    {
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, connection);
        adapter.Fill(dataset);
        connection.Close();
    }
        return dataset;
}

public void GridViewBind()
{
    string query = makequery();
    DataSet ds = GetDataset(query); // Updated
    if (ds != null)
    {
        if (ds.Tables[0].Rows.Count != 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}

您的查询看起来也不准确,可能不会执行。对于 1,你不能AND直接在之前ORDER BY

您的查询实际上很混乱,您尝试按条件排序,这实际上是不正确的。您想改用该WHERE子句。

query = "SELECT * FROM Form3 WHERE CONVERT(VARCHAR(25), [Datetime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "' ORDER BY [Datetime] DESC";

我认为这个站点将帮助您使用 SQL这个站点使用 DataSet

于 2013-04-05T08:25:53.543 回答
0

这是一个简单的方法:

private DataTable LoadData()
{
    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["yourConnectionStringName"].ToString());
    SqlCommand command = new SqlCommand();

    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "yourProcName";

    command.Connection = connection;
    connection.Open();
    SqlDataAdapter da = new SqlDataAdapter(command);
    da.Fill(dt);
    connection.Close();

    return dt;
}

private void BindGrid()
{
    GridView1.DataSource = this.LoadData();
    GridView1.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.BindGrid();
    }
}

修改上面的代码以满足您的需要。我习惯stored procedure加载数据,你也可以使用inline查询。inline如果要使用查询,只需修改这两行。

command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM yourTable";
于 2013-04-05T08:32:29.233 回答
0

您的所有查询都使用“AND order by yourColumns”。我不认为'AND'是必要的。它以这种方式工作(注意没有AND)

select ColumnA, ColumnB from yourTable where ColumnA='abc' order by ColumnB

此外,您没有执行查询,因此没有结果。

于 2013-04-05T09:06:29.443 回答
0

我发现了问题所在。网格视图的背景颜色与字母颜色相同,因此它不可见......对不起,因为那个简单的事情打扰了你......

于 2013-12-31T10:02:51.603 回答
0

这也有效,真的很简单

dataGridView1.DataSource = dataset.Tables[0];

无需为桌面应用程序设置任何其他参数。这也将填充列名。

于 2014-06-14T07:30:09.123 回答