Need some help with Grid view

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Starr
    New Member
    • Mar 2011
    • 2

    Need some help with Grid view

    I'm trying to do the following.
    I want to use a gridview to load a set of associated
    records dynamically based upon the input of the user.
    I want the gridview to have a checkbox to the left of
    the records and allow the user to use this to select
    records to remove. The gridview will have pages and will
    be sortable using the columns.

    I had this working using a delete button, but when I went to using the checkbox it stopped sorting.

    I'll try to make this a bit clearer.
    1) A user enters in a bulk list of customer Ids

    2) This add the customers to the grid, ID#, First Name, Last, etc.

    3) After adding the customers, the user can then remove
    selected customers. Originally this was done by the delete record link (which worked) but they want to be able to change their mind and use a checkbox to select and unselect which ones to delete.

    4) Preferably they can page up and down the list, sort the list, etc while preserving the checkbox status, but they will live with a single page operation.

    5) They want to be able to sort records based on columns.
    I had this working before, but once I went to the checkbox version the sorting functioning broke!

    So, what I'm seeking is an example of a page, using the gridview, with checkboxs and sortable columns that is not tied to a data source but is dynamically populated.
    And if it makes a difference, I have the grid in a container of a website using a master page.
    (Example of some of the code that I was using before to
    create the original pages.)

    Code:
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Me.IsPostBack = False Then
                If Session("dtNewIntcodes") IsNot Nothing Then
                    Dim dt As DataTable = FormatProjectDataTable()
                    dt = Session("dtNewIntcodes")
                    gvCollectionList.DataSource = dt.DefaultView
                    gvCollectionList.DataBind()
                End If
            End If
    
        End Sub
    
      Protected Function FormatProjectDataTable() As DataTable
            Dim dt As DataTable = New DataTable()
            ' Create Columns
            dt.Columns.Add("Intcode", System.Type.GetType("System.String"))
            dt.Columns.Add("First Name", System.Type.GetType("System.String"))
            dt.Columns.Add("Last Name", System.Type.GetType("System.String"))
    
            Dim PKColumn(0) As DataColumn
    
            PKColumn(0) = dt.Columns("Intcode")
            dt.PrimaryKey = PKColumn
    
    
            Return dt
        End Function
    
        Private Sub AddInterview(ByVal Intcode As Integer)
    
            Dim dt As DataTable = FormatProjectDataTable()
            Dim dr As DataRow
    
            Dim Conn As String
            Conn = "..........." ' Removed for example
    
            'Create a connection to the database
            Dim objConn
            objConn = Server.CreateObject("ADODB.Connection")
            objConn.Open(Conn)
    
    
            If Session("dtNewIntcodes") IsNot Nothing Then dt = Session("dtNewIntcodes")
    
            Dim strSQL As String
            strSQL = "SELECT * FROM Custtable where intcode = " & Intcode.ToString 
    
            Dim objRS
            objRS = Server.CreateObject("ADODB.Recordset")
            objRS.Open(strSQL, objConn)
            '
    
            Do While Not objRS.EOF
                dr = dt.NewRow
                dr("Intcode") = Right("     " + CStr(objRS("Intcode").value), 6)
                dr("First Name") = objRS("FirstName").value
                dr("Last Name") = objRS("LastName").value
                dt.Rows.Add(dr)
                objRS.MoveNext()
            Loop
            dt.AcceptChanges()
    
            objConn.close()
    
            Session("dvNewIntcodes") = dt.DefaultView
            Session("dtNewIntcodes") = dt
            gvCollectionList.DataSource = dt.DefaultView
            gvCollectionList.DataBind()
    
        End Sub
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    What you need is to put some code in each event. e.g. keep a record of the selected rows in the 'select row' event of the grid, make a sorted query in the sort event of the grid, change the grid page in the 'page change' event... and so on.

    It's kinda boring, but it isn't that hard to do.

    Comment

    • Richard Starr
      New Member
      • Mar 2011
      • 2

      #3
      Thanks, but I do have the events, though perhaps they are
      now malformed. Like I noted, it was working UNTIL I added
      the checkbox using the asp template method. After that,
      the hyperlinks that trigger the sorting disappeared.

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Thats weird...

        Check you're allowing sorting and validation. Also check the columns you can sort have a sorting expresion you'll later retrieve in you code.

        Here's an excercise I made with some columns that can be sorted and some that can't, a couple of buttons and a template:

        Code:
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="margin-left: 0px; font-size: small; text-align: center;" Width="1025px" Height="16px" AllowPaging="True" AllowSorting="True" EnableModelValidation="True">
            <RowStyle Font-Bold="False" />
            <Columns>
                <asp:BoundField DataField="id_inc" HeaderText="id" SortExpression="id_inc" />
                <asp:BoundField DataField="descrip" HeaderText="descripción" SortExpression="descrip" />
                <asp:BoundField DataField="producto" HeaderText="producto" SortExpression="producto" />
                <asp:BoundField DataField="fecha_alta" HeaderText="agregado"  SortExpression="fecha_alta" dataformatstring="{0:dd-MMM-yyyy}"/>
                <asp:BoundField DataField="fecha_comp" HeaderText="compromiso" SortExpression="fecha_comp" dataformatstring="{0:dd-MMM-yyyy}"/>
                <asp:BoundField DataField="estado" HeaderText="estado" SortExpression="estado" />
                <asp:BoundField DataField="responsable" HeaderText="responsable" SortExpression="responsable"/>
                <asp:CommandField ButtonType="Image" EditImageUrl="~/edit.png" ShowEditButton="True" />
                <asp:CommandField ButtonType="Image" SelectImageUrl="~/info.png" ShowSelectButton="True" />
                <asp:TemplateField>
                    <ItemTemplate><asp:CheckBox ID="CheckBox1" runat="server" /></ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle Font-Bold="True" Font-Italic="False" ForeColor="#666666" />
            <HeaderStyle Font-Bold="True" ForeColor="#666666" />
        </asp:GridView>
        Sorry about the spanish. It works perfectly, perhaps you can use it to compare what elements you're missing.

        Comment

        Working...