gridview的ID显示错误的序列

时间:2014-04-28 05:42:10

标签: c# asp.net gridview

我有一个gridview,它有列(Id,Task,Reward,Time Allotted,Uploader)..我可以成功地将数据插入gridview,直到第13页的taskId 118,但之后数据开始显示在在TaskId 7之后的第1页。

所以序列就像.... 5,6,7,119,120,121,122,8,9,10 ..... 所以所有的新数据都插在7到8之间。为什么?我怎么能纠正这个?

我不认为这是代码错误,但仍然是我的:

    protected void btnPost_Click(object sender, EventArgs e)
{
    string CS = ConfigurationManager.ConnectionStrings["EasyRozMoney_ConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("spTasks", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@Title", txtTitle.Text);
        cmd.Parameters.AddWithValue("@Body", txtBody.Text);
        cmd.Parameters.AddWithValue("@Reward", txtRewards.Text);
        cmd.Parameters.AddWithValue("@TimeAllotted", txtTime.Text);
        cmd.Parameters.AddWithValue("@PosterName", txtPoster.Text);

        con.Open();
        cmd.ExecuteNonQuery();
        lblStatus.Text = "Task Posted Successfully.";

    }

这是gridview:

       <asp:GridView ID="GridView1" runat="server" Width="936px" AllowPaging="True" AutoGenerateColumns="False" CellPadding="3" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" style="text-align: center" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:BoundField DataField="TaskId" HeaderText="TaskId" InsertVisible="False" ReadOnly="True" SortExpression="TaskId" />
                    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                    <asp:BoundField DataField="Body" HeaderText="Body" SortExpression="Body" Visible="false" />
                    <asp:BoundField DataField="Reward" HeaderText="Reward(Rs)" SortExpression="Reward" />
                    <asp:BoundField DataField="TimeAllotted" HeaderText="Time(Min)" SortExpression="TimeAllotted" />
                    <asp:BoundField DataField="PosterName" HeaderText="Uploader" SortExpression="PosterName" />
                    <asp:ButtonField ButtonType="Button" CommandName="Select" Text="Perform Task" ControlStyle-ForeColor="White"  ControlStyle-Font-Bold="true">
               <ControlStyle BackColor="#CC6600" Font-Bold="True" ForeColor="White"></ControlStyle>
                    </asp:ButtonField>
                </Columns>
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <HeaderStyle BackColor="#CC6600" Font-Bold="True" ForeColor="White" />
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FFF1D4" />
                <SortedAscendingHeaderStyle BackColor="#B95C30" />
                <SortedDescendingCellStyle BackColor="#F1E5CE" />
                <SortedDescendingHeaderStyle BackColor="#93451F" />
            </asp:GridView>

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EasyRozMoney_ConnectionString %>" SelectCommand="SELECT * FROM [Task]"></asp:SqlDataSource>

我在sql中的表格如下:

       Create table Task
       (
       TaskId int Identity(1,1),
       Title varchar(100),
       Body varchar(500),
       Reward decimal(4,2),
       TimeAllotted int,
       PosterName varchar(100)
       )

我的存储过程:

      Create proc spTasks
      @Title varchar(100),
      @Body varchar(500),
      @Reward decimal(4,2),
     @TimeAllotted int,
      @PosterName varchar(100)
      as
       begin
       Insert into Task values(@Title,@Body,@Reward,@TimeAllotted,@PosterName)
       end

1 个答案:

答案 0 :(得分:1)

您可以更改数据源的select语句的默认排序。如果您希望最近的记录显示在最后:默认排序,如果您希望它们首先显示在您的网格中:按降序排序。

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:EasyRozMoney_ConnectionString %>" 
 SelectCommand="SELECT * FROM [Task] ORDER BY TASKID"></asp:SqlDataSource>

在备注中,您应该使用select&#39; *&#39;仅在测试用例中。在现实世界的场景中,这将导致执行表扫描的性能下降。在select语句中,只选择在UI中显示所需的列。

正如其他人指出的那样,请检查数据库列中int或varchar类型,如果以后可以将列转换为int,或者更好地将其创建为int。