这是我的代码.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
将gridview
与dataset
绑定dataset
无法找到表0.
我想要的是当我选择字段并按搜索按钮时,我可以根据所选字段查看网格中的详细信息,但我无法将其与{{1}}绑定。你能帮助我吗?
答案 0 :(得分: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();
}
答案 1 :(得分: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";
答案 2 :(得分: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";
答案 3 :(得分:0)
您的所有查询都使用'按YourColumns排序'。我不认为'AND'是必要的。它以这种方式工作(注意没有AND)
select ColumnA, ColumnB from yourTable where ColumnA='abc' order by ColumnB
此外,您没有执行查询,因此没有结果。
答案 4 :(得分:0)
我发现了什么问题。 gridview的背景颜色与字母颜色相同,因此它不可见..... 我很抱歉因为那个简单的事情我打扰了你...... < / p>
答案 5 :(得分:0)
这也很有效,非常简单
dataGridView1.DataSource = dataset.Tables[0];
无需为桌面应用程序设置任何其他参数。这也会填充列名。