3 Replies - 3567 Views - Last Post: 05 March 2013 - 03:14 PM Rate Topic: -----

#1 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Asp.Net Gridview Commandfield issues

Posted 05 March 2013 - 09:35 AM

hey fellas,

I'm working on manipulating the gridview to open a different kind of database system that I use called Codebase. All is going well, but when I click the edit button after it's autogenerated the update button does not show. Any ideas why?

Here is my code so far

 Private Sub MyGridView_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles Me.RowEditing
        Me.EditIndex = e.NewEditIndex
Me.Rows(Me.EditIndex).RowState = DataControlRowState.Edit
End Sub

 Private Sub MyGridView_RowCancelingEdit(sender As Object, e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles Me.RowCancelingEdit
        Me.EditIndex = -1
        e.Cancel = True
        Me.Rows(e.RowIndex).RowState = DataControlRowState.Normal
    End Sub



Would databinding cause any problems?


I figured it out! I forgot to Databind after setting my new edit index on my gridview! I feel so darn stupid now lol, but hopefully somebody else finds this extremely useful.

This post has been edited by trevster344: 05 March 2013 - 04:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Asp.Net Gridview Commandfield issues

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Asp.Net Gridview Commandfield issues

Posted 05 March 2013 - 10:45 AM

can you show the code for the GridView?
Was This Post Helpful? 0
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Asp.Net Gridview Commandfield issues

Posted 05 March 2013 - 01:37 PM

Sure no problem. I inherited the gridview into my own class.

Public Class CodebaseGridView
    Inherits GridView

    Dim MyTable As DataTable

    Dim Database_Path As String

    Dim ErrorMessage As String


    Dim FirstField As Long
    Dim FirstFieldString As String
    Dim MyField As New List(Of Long)

    Dim SelectedRowIndex As Integer
    Dim WithEvents AddCell As Button

    Dim MyCommandField As CommandField

    Dim Generated As Boolean = False

    Dim InsertTextBoxCount As Integer = 0
    Dim EditTextBoxCount As Integer = 0

    Public Property DatabasePath As String
        Get
            Return Database_Path
        End Get
        Set(value As String)
            Database_Path = value
        End Set
    End Property

    Public Property Error_Message As String
        Get
            Return ErrorMessage
        End Get
        Set(value As String)
            ErrorMessage = value
        End Set
    End Property

    Public Sub New(ByVal DBPath As String)
        MyTable = New DataTable

        Database_Path = DBPath

        Me.EnableViewState = True
        Me.ViewStateMode = UI.ViewStateMode.Enabled

        Me.CellPadding = 4

        Me.GridLines = WebControls.GridLines.None
        Me.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333")

        Me.AlternatingRowStyle.BackColor = Drawing.Color.White

        Me.FooterStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#990000")
        Me.FooterStyle.ForeColor = Drawing.Color.White
        Me.FooterStyle.Font.Bold = True

        Me.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#990000")
        Me.HeaderStyle.Font.Bold = True
        Me.HeaderStyle.ForeColor = Drawing.Color.White

        Me.PagerStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFCC66")
        Me.PagerStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333")
        Me.PagerStyle.HorizontalAlign = WebControls.HorizontalAlign.Center

        Me.RowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFBD6")
        Me.RowStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333")

        Me.SelectedRowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFCC66")
        Me.SelectedRowStyle.Font.Bold = True
        Me.SelectedRowStyle.ForeColor = Drawing.Color.Navy

        Me.SortedAscendingCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#FDF5AC")
        Me.SortedAscendingHeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#4D0000")
        Me.SortedDescendingCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#FCF6C0")

        Me.SortedDescendingHeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#820000")

        If Generated = False Then
            Me.DataSource = LoadTable()

            Me.DataBind()
        End If

    End Sub

    Private Function LoadTable() As DataTable

        Dim DataTable1 As New DataTable

        Dim MyDb, MyCb As Long
        Dim MyRc As Integer

        OpenAnyDb(Database_Path, MyCb, MyDb, MyRc, 0) 'Open the database and supply the reference values of the database path and name, and the long variables to store the pointers to the database

        FirstField = d4fieldJ(MyDb, 1) 'First Field Pointer
        FirstFieldString = f4name(FirstField) ' First Field Description String

        d4tagSelect(MyDb, d4tag(MyDb, FirstFieldString)) 'Selecting the First Field as Index Pointer

        Dim FieldList As New List(Of String) 'List that will contain all the field names

        Dim FieldPointer As Long 'Field Pointer that will hold each pointer as we run through the fields list

        For i = 1 To d4numFields(MyDb) 'Add the pointers of each field in the database to the fieldlist
            FieldPointer = d4fieldJ(MyDb, i)
            FieldList.Add(f4name(FieldPointer)) 'Add the field name to the field array
            MyField.Add(FieldPointer) 'Add the pointer into the myfield array for grabbing data
        Next

        For i = 0 To FieldList.Count - 1

            DataTable1.Columns.Add(FieldList(i))

        Next

        MyRc = d4top(MyDb)

        Do While MyRc = 0

            If MyRc = 0 And d4deleted(MyDb) <> 1 Then

                If Trim(f4str(MyField(1) & "")).Length <= 0 Then GoTo skipit

                Dim FieldData(FieldList.Count - 1) As String 'Row'

                For i = 0 To FieldList.Count - 1
                    FieldData(i) = Trim(f4str(MyField(i) & ""))
                Next

                DataTable1.Rows.Add(FieldData)

            End If
SkipiT:
            MyRc = d4skip(MyDb, 1)

        Loop

        OpenAnyDb(Database_Path, MyCb, MyDb, MyRc, 1)

        SetGeneratedButtons()

        Return DataTable1
    End Function

    Private Sub SetGeneratedButtons()
        Me.ShowFooter = True
        If Generated = False Then
            MyCommandField = New CommandField

            With MyCommandField
                .Visible = True
                .UpdateText = "Update"
                .ShowEditButton = True
                .ShowDeleteButton = True
                .ShowCancelButton = True
                '.ShowInsertButton = True
                .EditText = "Edit"
                .DeleteText = "Delete"
                .CancelText = "Cancel"
                .ButtonType = ButtonType.Link
            End With
           
            Me.Columns.Add(MyCommandField)

            Generated = True
        End If
    End Sub

    Private Sub CodebaseGridView_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        Me.SaveControlState()
    End Sub

    Private Sub MyGridView_RowCancelingEdit(sender As Object, e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles Me.RowCancelingEdit
        Me.EditIndex = -1
        e.Cancel = True
        Me.Rows(e.RowIndex).RowState = DataControlRowState.Normal
    End Sub

    Private Sub UpdateDatabase(ByVal EditValues As List(Of String))

        Dim FindWhat As String = EditValues(0)
        Dim FoundIt As Integer
        Dim Seekcnt As Integer

        Dim TheDB, TheCB As Long
        Dim TheRc As Integer

        Call OpenAnyDb(Database_Path, TheCB, TheDB, TheRc, 0)

        Call d4tagSelect(TheDB, d4tag(TheDB, FirstFieldString))

        Dim Fields As New List(Of String)
        Dim FieldPointer As Long
        Dim PMyFields As New List(Of Long)

        For i = 0 To d4numFields(TheDB)
            FieldPointer = d4fieldJ(TheDB, i)
            Fields.Add(f4name(FieldPointer))
            PMyFields.Add(FieldPointer)
        Next

        TheRc = d4top(TheDB)

        Do While TheRc = 0

            If Seekcnt = 0 Then
                TheRc = d4seek(TheDB, FindWhat)
                Seekcnt = 1
            Else
                TheRc = d4seekNext(TheDB, FindWhat)
            End If

            If TheRc = 0 And d4deleted(TheDB) <> 1 Then
                For i = 0 To PMyFields.Count - 1
                    If Trim(f4str(PMyFields(i) & "")) = EditValues(i) Then
                        Exit Do
                    End If
                Next

                FoundIt = 1

            End If
        Loop

        For i = 0 To PMyFields.Count - 1
            f4assign(PMyFields(i), EditValues(i))
        Next

        Call d4flush(TheDB)
        Call d4unlock(TheDB)

        Call OpenAnyDb(Database_Path, TheCB, TheDB, TheRc, 1)

    End Sub

    Private Sub CodebaseGridView_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Me.RowCommand
        If e.CommandName = "Update" Then
            Dim EditValues As New List(Of String)

            For i = 1 To Me.Rows(Me.EditIndex).Cells.Count - 1
                For t = 0 To Me.Rows(Me.EditIndex).Cells(i).Controls.Count - 1
                    If TypeOf Me.Rows(Me.EditIndex).Cells(i).Controls(t) Is TextBox Then
                        EditValues.Add(CType(Me.Rows(Me.EditIndex).Cells(i).Controls(t), TextBox).Text)
                    End If
                Next
            Next

            Me.EditIndex = -1

            UpdateDatabase(EditValues)

            Me.DataSource = LoadTable()
            Me.DataBind()
        End If
    End Sub

    Private Sub CodebaseGridView_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Me.RowDataBound
        If e.Row.RowType = DataControlRowType.Footer Then
            If e.Row.Cells(0).Controls.Count <= 0 Then
                AddCell = New Button
                AddCell.Text = "Add Row"
                AddCell.UseSubmitbehavior = False
                e.Row.Cells(0).Controls.Add(AddCell)

                Dim NewTextBox As TextBox
                Dim TextboxCount As Integer = 1

                For i = 1 To e.Row.Cells.Count - 1
                    NewTextBox = New TextBox
                    NewTextBox.ID = "AddTxt" & TextboxCount.ToString
                    e.Row.Cells(i).Controls.Add(NewTextBox)
                    TextboxCount += 1
                Next

                InsertTextBoxCount = TextboxCount

            End If
        End If
    End Sub

    Private Sub CodebaseGridView_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles Me.RowDeleting
        Dim DeleteID As String = e.RowIndex
    End Sub

    Private Sub MyGridView_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles Me.RowEditing
        Me.EditIndex = e.NewEditIndex

        Dim NewTextBox As TextBox

        Dim TextboxCount As Integer = 0

        For i = 1 To Me.Rows(Me.EditIndex).Cells.Count - 1
            NewTextBox = New TextBox
            NewTextBox.ID = "EditTxt" & TextboxCount.ToString
            NewTextBox.Text = Me.Rows(Me.EditIndex).Cells(i).Text
            Me.Rows(Me.EditIndex).Cells(i).Text = ""
            Me.Rows(Me.EditIndex).Cells(i).Controls.Add(NewTextBox)
            TextboxCount += 1
        Next

        Me.Rows(Me.EditIndex).RowState = DataControlRowState.Edit
        EditTextBoxCount = TextboxCount

    End Sub

    Private Sub AddCell_Click(sender As Object, e As System.EventArgs) Handles AddCell.Click

        Dim FieldValues As New List(Of String)

        FieldValues.Add(" ")

        For i = 1 To InsertTextBoxCount - 1

            Dim TempText As TextBox = CType(Me.FooterRow.Cells(i).Controls(0), TextBox)
            If TempText.Text.Length >= 1 Then
                FieldValues.Add(TempText.Text)
            Else
                Exit Sub
            End If

        Next

        FieldValues.RemoveAt(0)

        'Datatable.Rows.Add(FieldValues.ToArray)

        AddToDatabase(FieldValues.ToArray)

        Dim Datatable As DataTable = LoadTable()

        Me.DataSource = Datatable
        Me.DataBind()
    End Sub

    Private Sub AddToDatabase(ByVal FieldValues() As String)

        Dim TheDB, TheCB As Long
        Dim TheRc As Integer

        Call OpenAnyDb(Database_Path, TheCB, TheDB, TheRc, 0)

        Call d4tagSelect(TheDB, d4tag(TheDB, FirstFieldString))

        Dim FieldPointer As Long

        Dim PMyField As New List(Of Long)

        For i = 1 To d4numFields(TheDB) '
            FieldPointer = d4fieldJ(TheDB, i) '
            PMyField.Add(FieldPointer)
        Next

        TheRc = d4top(TheDB)

        TheRc = d4appendBlank(TheDB)

        For i = 0 To FieldValues.GetUpperBound(0)
            f4assign(PMyField(i), FieldValues(i))
        Next

        Call d4flush(TheDB)
        Call d4unlock(TheDB)

        Call OpenAnyDb(Database_Path, TheCB, TheDB, TheRc, 1)
    End Sub
End Class


In this code I've tried making my own commandfield just to see if manually setting values would do anything but alas the same results again. I'm pretty new with this level of manipulation in a Gridview so all tips are welcome.

I've done this before but only using SQL and the datasources, etc so this time I needed it for Codebase and flipped it around a bit. All is working, I can insert records, and such but I can't seem to get the Commandfield to operate correctly otherwise I could finish wiring in the code properly. Any help is appreciated.

This post has been edited by trevster344: 05 March 2013 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Asp.Net Gridview Commandfield issues

Posted 05 March 2013 - 03:14 PM

While I never did figure out the right way, my gridview does maintain variables on postback so I created a boolean for the edit event, and based on specific conditions I'll change the commandfield text, and simulate my own update button using the edit button. Though if you do have any suggestions as to what might be my issue, do tell! Thank you. :)/>

-Read First Post-

This post has been edited by trevster344: 05 March 2013 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1