在gridview中显示两个日期之间的数据 [英] Showing data between two dates in gridview

查看:57
本文介绍了在gridview中显示两个日期之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>View Docket Values </title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:Label ID="Date1" runat="server" Text="From Date"></asp:Label>
        <asp:TextBox ID="DateFrom" runat="server"></asp:TextBox>
        <asp:Label ID="Date2" runat="server" Text="To Date"></asp:Label>
        <asp:TextBox ID="ToDate" runat="server"></asp:TextBox>
        <asp:Button ID="Show" runat="server" Text="ShowData" OnClick="Show_Click" />
    </div>
<div>
<asp:GridView runat="server" ID="gvrecords"  AutoGenerateColumns="false" ShowFooter="true" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" DataKeyNames="DocketNo" Width="80px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href ='<%#"UpdateDocket.aspx?DocketNo="+DataBinder.Eval(Container.DataItem,"DocketNo") %>'> <%#Eval("DocketNo") %>  </a>
</ItemTemplate>
</asp:TemplateField>
    <asp:BoundField DataField="ConsignorName" HeaderText="Consigner" />
    <asp:BoundField DataField="ConsigneeName" HeaderText="Consignee" />
    <asp:BoundField DataField="Date" HeaderText="Booking Date" />
    <asp:BoundField DataField="FromSTN" HeaderText="Booking Station" />
    <asp:BoundField DataField="ToStn" HeaderText="Delivery Station" />
    <asp:BoundField DataField="PKG" HeaderText="No. of Packets" />
    <asp:BoundField DataField="ActualWt" HeaderText="Actual Weight" />
    <asp:BoundField DataField="ChargeWt" HeaderText="Charge Weight" />
    <asp:BoundField DataField="InvoiceValue" HeaderText="Invoice Value" />
    <asp:BoundField DataField="Basis" HeaderText="Basis" />
    <asp:BoundField DataField="FreightCharge" HeaderText="Freight Charge" />
    <asp:BoundField DataField="FuelSurcharge" HeaderText="Fuel Surcharge" />
    <asp:BoundField DataField="FOV" HeaderText="FOV" />
    <asp:BoundField DataField="COD" HeaderText="COD" />
    <asp:BoundField DataField="FOD" HeaderText="FOD" />
    <asp:BoundField DataField="PickupCharge" HeaderText="Pickup Charge" />
    <asp:BoundField DataField="DoorDelivery" HeaderText="Door Delivery" />
    <asp:BoundField DataField="Handling" HeaderText="Handling" />
    <asp:BoundField DataField="Misc" HeaderText="Misc" />
    <asp:BoundField DataField="DktCharge" HeaderText="Docket Charge" />
    <asp:BoundField DataField="SubTotal" HeaderText="Sub Total" />
    <asp:BoundField DataField="ServiceTax" HeaderText="Service Tax" />
    <asp:BoundField DataField="GrandTotal" HeaderText="Grand Total" />
    <asp:BoundField DataField="Service" HeaderText="Service" />
</Columns>
</asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:amitpandeyConnectionString %>" SelectCommand="SELECT * FROM [Docket]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>



和C#代码是


and the C# Code is

public partial class Docket : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    protected void BindGridview()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["amitpandeyConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Docket", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        cmd.ExecuteNonQuery();
        con.Close();
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvrecords.DataSource = ds;
        gvrecords.DataBind();
        DataSet db = new DataSet();
        da.Fill(db);
        gvrecords.Columns[7].FooterText = (from row in dt.AsEnumerable() select row.Field("ActualWt")).Skip(gvrecords.PageIndex * gvrecords.PageSize).Take(gvrecords.PageSize).Sum().ToString("C", ActualWt).Remove(0, 2).Trim();
        gvrecords.DataSource = ds;
        gvrecords.DataBind();

    }

    protected void Show_Click(object sender, EventArgs e)
    {

    }
}





我的问题是我想在文本框的帮助下选择两个日期,我想借助按钮显示这两个日期内的数据



my problem is I want to select two dates with the help of textbox and I want to show the data inside those two dates with the help of a button

推荐答案

ConnectionStrings:amitpandeyConnectionString %> SelectCommand = SELECT * FROM [Docket] > < / asp:SqlDataSource >
< / div >
< / form >
< / body < span class =code-keyword>>
< / html >
ConnectionStrings:amitpandeyConnectionString %>" SelectCommand="SELECT * FROM [Docket]"></asp:SqlDataSource> </div> </form> </body> </html>



和C#代码是


and the C# Code is

public partial class Docket : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    protected void BindGridview()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["amitpandeyConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Docket", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        cmd.ExecuteNonQuery();
        con.Close();
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvrecords.DataSource = ds;
        gvrecords.DataBind();
        DataSet db = new DataSet();
        da.Fill(db);
        gvrecords.Columns[7].FooterText = (from row in dt.AsEnumerable() select row.Field("ActualWt")).Skip(gvrecords.PageIndex * gvrecords.PageSize).Take(gvrecords.PageSize).Sum().ToString("C", ActualWt).Remove(0, 2).Trim();
        gvrecords.DataSource = ds;
        gvrecords.DataBind();

    }

    protected void Show_Click(object sender, EventArgs e)
    {

    }
}





我的问题是我想在文本框的帮助下选择两个日期,我想借助按钮显示这两个日期内的数据



my problem is I want to select two dates with the help of textbox and I want to show the data inside those two dates with the help of a button


为了防止sql注入,请始终使用Parameterised Query。

试试这样,

To prevent from sql injection, Always use Parameterised Query.
Try like this,
SqlCommand cmd = new SqlCommand("select * from Docket where yourDate between @FromDate and @ToDate", con);
//Declare it Globally
DateTime FromDate;
DateTime Todate;
System.Globalization.CultureInfo culmoney = new System.Globalization.CultureInfo("hi-IN",true);

FromDate = DateTime.Parse(txtFromDate.Text.Trim(), cul, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
                Todate = DateTime.Parse(txtTodate.Text.Trim(), cul, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
                cmd.Parameters.AddWithValue("@FromDate", FromDate.ToShortDateString());
                cmd.Parameters.AddWithValue("@ToDate", Todate.ToShortDateString());


试试这个.............

Try This.............
select * from Docket where [Date] between Convert(Datetime,'"+ TextBox1.Text +"',102) and Convert(Datetime,'"+ TextBox2.Text +"',102) 







Regards,

AARIF SHAIKH




Regards,
AARIF SHAIKH

这篇关于在gridview中显示两个日期之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆