locked
RowIndex on DataTable and DataView RowFilter RRS feed

  • Question

  • User-72198832 posted

    I have a DataTable with two DataRows

    Un-Edited Rows
    Index   Description    Control
    0         Red Box            P
    1         Blue Shoes        P

    Here's how I get RowIndex
    Protected Sub Partsview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles PartsView1.RowCommand
    Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument.ToString())

    I want to Hide a DataRow so I store a "D" in the Control Column
    dtParts.Rows(currentRowIndex)("Control") = "D"

    I also use a RowFilter to hide all rows with"D" in Control field

    Dim dv As New DataView(dtParts)
    dv.RowFilter = "Control <> 'D'"
    PartsView1.DataSource = dv
    PartsView1.DataBind()

    If I first hide rowindex 1 using code below row is hidden properly

    dtParts.Rows(currentRowIndex)("Control") = "D"

    If I then hide rowindex 0 it too is hidden

    My rows
    Index   Description    Control
    0         Red Box            D
    1         Blue Shoes        D

    All is OK at this point.

    Now Repeating this Process  Using UnEdited Data from top of this post

    Here is problem...

    Hide Row 0 first Then Hide remaining visible row
    Data table is then
    Index   Description    Control
    0         Red Box            D
    1         Blue Shoes        P
    Row does not get hidden because Im trying to hide Index 1 but since it's the only displayed row Im overwriting Control field in Index 0 with "D"

    After Hiding the Red Box row Blue Shoes moves into displayed row index position 0 but in reality it's still in table as row 1
    How do I Update a table row if I can't get proper Row Index?

    Any help is appreciated

    LB

    Wednesday, January 15, 2020 7:43 AM

All replies

  • User283571144 posted

    Hi TRIMS30,

    According to your description and codes, I found you has the Index column. If the index column is an unique and auto-increment value, I suggest you could use it instead of the "e.CommandArgument".

    More details ,you could refer to below codes:

    aspx page:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="GridViewParts.aspx.vb" Inherits="VBIssue.GridViewParts" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="PartsView1" runat="server" AutoGenerateColumns="false" OnRowCommand="PartsView1_RowCommand">
                    <Columns>
                        <asp:BoundField DataField="Index" ShowHeader="true" HeaderText="Index" />
                        <asp:BoundField DataField="Description" ShowHeader="true" HeaderText="Description" />
                        <asp:BoundField DataField="Control" ShowHeader="true" HeaderText="Control" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="HideBtn" runat="server" CommandName="Hide" CommandArgument='<%#Eval("Index") %>' Text="Hide"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    

    Code-Behind:

    Imports System
    Imports System.Web.UI.WebControls
    Imports System.Data
    
    Public Class GridViewParts
        Inherits System.Web.UI.Page
    
        Private Shared _dt As DataTable
    
        Public Shared ReadOnly Property dtParts As DataTable
            Get
    
                If _dt Is Nothing Then
                    Dim dt As DataTable = New DataTable("TestTable")
                    dt.Columns.Add("Index", GetType(Integer))
                    dt.Columns.Add("Description", GetType(String))
                    dt.Columns.Add("Control", GetType(String))
                    dt.Rows.Add(0, "Red Box", "P")
                    dt.Rows.Add(1, "Blue Shoes", "P")
                    _dt = dt
                End If
    
                Return _dt
            End Get
        End Property
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                BindDTParts()
            End If
        End Sub
    
        Public Sub BindDTParts()
            Dim dv As DataView = New DataView(dtParts)
            dv.RowFilter = "Control <> 'D'"
            PartsView1.DataSource = dv
            PartsView1.DataBind()
        End Sub
    
        Protected Sub PartsView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
            Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument.ToString())
            dtParts.Rows(currentRowIndex)("Control") = "D"
            BindDTParts()
        End Sub
    End Class
    

    Result:

    Best Regards,

    Brando

    Thursday, January 16, 2020 9:44 AM