排序允许编辑的GridView文本框列

时间:2015-07-30 15:35:09

标签: asp.net vb.net sorting gridview

我有一个包含5列的GridView。两个是文本框字段,两个是下拉字段,一个是行序列号。 AllowSorting设置为" true"

每个字段都是一个具有各自SortExpressions的TemplateField。 ASP页面看起来像这样:

<asp:GridView ID="GVDoc" runat="server" AllowPaging="false" AllowSorting="true" AutoGenerateColumns="false" OnRowDataBound="GVDoc_ItemDataBound">
    <asp:TemplateField HeaderText="Sequence" SortExpression="Sequence" >
        <ItemTemplate>
            <asp:Label ID="lblOrder" Text='<%# Eval("Sequence")%>' runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Property (Yes/No)" SortExpression="Property" >
        <ItemTemplate>
            <asp:DropDownList ID="ddlProperty" runat="server" DataValueField='<%# Eval("Property")%>'>
                <asp:ListItem Value="No">No</asp:ListItem>
                <asp:ListItem Value="Yes">Yes</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Valid (Yes/No)" SortExpression="Valid">
        <ItemTemplate>
           <asp:DropDownList ID="ddlValid" runat="server" DataValueField='<%# Eval("Valid")%>'>
                <asp:ListItem Value="No">No</asp:ListItem>
                <asp:ListItem Value="Yes">Yes</asp:ListItem>
           </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name" SortExpression="Name" >
        <ItemTemplate>
            <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Description" SortExpression="Description" >
        <ItemTemplate>
            <asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("Description")%>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

每个字段都从数据库中填充。允许用户编辑文本并更改下拉值。编辑的数据不会在数据库中更新。它只是能够在屏幕上看到。

我需要允许在每列的基础上进行排序。我可以对所有列进行排序。

背后的代码如下:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As DataTable
        Dim sql As String = "SELECT ROW_NUMBER() OVER(ORDER BY File_Id) as Sequence, 'No' as Property, 'No' as Valid, File_Id, Name, Description FROM FILE

        Using conn As SqlConnection = New SqlConnection(...ConnectionString...)
            Using comm As SqlCommand = New SqlCommand(sql, conn)
                conn.Open()
                Using da As SqlDataAdapter = New SqlDataAdapter(comm)
                    dt = New DataTable("table")
                    da.Fill(dt)
                End Using
            End Using
        End Using
        GVDoc.DataSource = dt
        GVDoc.DataBind()
        Cache("ABC") = dt
        ViewState("Sequence") = "Sequence ASC"
        ViewState("Property") = "Property ASC"
        ViewState("Valid") = "Valid ASC"
        ViewState("Name") = "Name ASC"
        ViewState("Description") = "Description ASC"            
    End If
End Sub

Protected Sub GVDoc_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GVDoc.Sorting
    If ViewState("Sequence").ToString().Contains(e.SortExpression) Then
        If ViewState("Sequence").ToString().Contains("ASC") Then
            RebindData(e.SortExpression, "DESC", "Sequence")
        Else
            RebindData(e.SortExpression, "ASC", "Sequence")
        End If
    ElseIf ViewState("Property").ToString().Contains(e.SortExpression) Then
        If ViewState("Property").ToString().Contains("ASC") Then
            RebindData(e.SortExpression, "DESC", "Property")
        Else
            RebindData(e.SortExpression, "ASC", "Property")
        End If
    ElseIf ViewState("Valid").ToString().Contains(e.SortExpression) Then
        If ViewState("Valid").ToString().Contains("ASC") Then
            RebindData(e.SortExpression, "DESC", "Valid")
        Else
            RebindData(e.SortExpression, "ASC", "Valid")
        End If
    ElseIf ViewState("Name").ToString().Contains(e.SortExpression) Then
        If ViewState("Name").ToString().Contains("ASC") Then
            RebindData(e.SortExpression, "DESC", "Name")
        Else
            RebindData(e.SortExpression, "ASC", "Name")
        End If
    ElseIf ViewState("Description").ToString().Contains(e.SortExpression) Then
        If ViewState("Description").ToString().Contains("ASC") Then
            RebindData(e.SortExpression, "DESC", "Description")
        Else
            RebindData(e.SortExpression, "ASC", "Description")
        End If
    End If
End Sub

Private Sub RebindData(ColumnName As String, SortOrder As String, SortExpression As String)
    Dim dt As DataTable = CType(Cache("ABC"), DataTable)
    dt.DefaultView.Sort = ColumnName + " " + SortOrder
    GVDoc.DataSource = dt
    GVDoc.DataBind()
    ViewState(SortExpression) = ColumnName + " " + SortOrder
End Sub

我找到了关于不同排序方式的各种文章,我可以对它进行排序。

但我的实际问题是,我可以通过这种方式对每列进行排序。但是当我更改下拉值或文本框值然后尝试按任何列排序时,它会排序并显示我从数据库中获取的数据,而不是我在排序之前编辑的数据。编辑后的数据消失了。

即使在排序后,有没有办法保留已编辑的数据?我不想更新数据库。

2 个答案:

答案 0 :(得分:2)

编辑数据后,我更新了DataTable,将DataTable重新分配给Cache,然后调用了DataBind()。

要保留已编辑的下拉字段值,请更新RowDataBound中DataTable的下拉字段。

感谢您的解决方案。

答案 1 :(得分:1)

如果您编辑数据表的值,那么您应该将DataTable更新为缓存。

然后调用你的RebindData它应该工作。

也请发布行更新功能。这将更清楚地了解编辑后发生的事情。