Grid View - disconnected datasource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sspost
    New Member
    • Sep 2007
    • 15

    #1

    Grid View - disconnected datasource

    i used Grid view and here is the code i wrote:
    [code=vbnet]
    Protected Sub ButtonAddFile_C lick(ByVal sender As Object, ByVal e As EventArgs)
    Dim gv As New GridView
    gv.Style.Add("p osition", "absolute")
    gv.Style.Add("l eft", "575px")
    gv.Style.Add("t op", "220px")
    gv.EnableViewSt ate = False
    form1.Controls. Add(gv)
    gv.DataSource = GetDataTable()
    gv.DataBind()
    Dim lbl As New Label
    lbl.Style.Add(" position", "absolute")
    lbl.Style.Add(" left", "575px")
    lbl.Style.Add(" top", "20px")
    End Sub
    Private Function GetDataTable() As DataTable
    Dim doc As New DataTable("Docu ment")
    Dim id As New DataColumn("Doc ument")

    id.DataType = GetType(String)
    id.Unique = True
    id.AllowDBNull = False
    id.Caption = "Document"

    doc.Columns.Add (id)

    Dim filing As New DataColumn("Typ e")
    filing.DataType = GetType(String)
    'filing.MaxLeng th = 35
    filing.AllowDBN ull = False

    doc.Columns.Add (filing)
    doc.PrimaryKey = New DataColumn() {id}
    'add data to data table

    Dim newdoc As DataRow = doc.NewRow()
    newdoc("Documen t") = FileToUpload.Po stedFile.FileNa me
    newdoc("Type") = listDocType.Sel ectedValue

    doc.Rows.Add(ne wdoc)
    Return doc
    End Function
    [/code]
    1. But it adds only one row everytime i click the AddFile button and overrides the previous row, it doesnt add on after another, whats going wrong here?
    2. Also If I have to add a delete button of GridView , what code should i write so that it deletes that particular row every time i click delete
    Last edited by Frinavale; Sep 6 '07, 05:54 PM. Reason: Added [code] tags and made code more legible
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Hi Sspost,

    I have moved your question to the .NET Forums section. The .NET Articles section is reserved for "how-tos" and other informational articles and is not meant for your questions. You will receive help with your problems in the .NET Forums section so please post any further questions here (Blue menu: Forums->.NET)

    Thanks,

    -Moderator

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      You need to be adding your rows to your dataobject, everytime you change the datasource property it wipes out all your old values and re-creates from the new ones.
      Maintain a DataTable object and add rows to that.

      Comment

      • sspost
        New Member
        • Sep 2007
        • 15

        #4
        [Ok , I used dataobject and can add rows one after another, but i added delete button but still cant get the delete to delete the rows from gridview ...here is the code i wrote:
        [code=html]
        <asp:datagrid id="DataGrid1" runat="server" Width="100%" AutoGenerateCol umns="False">
        <AlternatingIte mStyle BackColor="beig e"></AlternatingItem Style>
        <ItemStyle Font-Size="Smaller" HorizontalAlign ="Center" BackColor="Ligh tGray"></ItemStyle>
        <HeaderStyle Font-Size="Smaller" Font-Bold="True" HorizontalAlign ="Center" ForeColor="Whit e"
        BackColor="DimG ray"></HeaderStyle>
        <Columns>
        <asp:BoundColum n DataField="Docu ment" HeaderText="Doc ument"></asp:BoundColumn >
        <asp:BoundColum n DataField="Docu ment Title" HeaderText="Doc ument Title"></asp:BoundColumn >
        <asp:ButtonColu mn commandname="De lete" headertext="Del ete" Text="Delete"></asp:ButtonColum n>
        </Columns>
        </asp:datagrid>
        [/code]
        here is the code:
        [code=vbnet]
        Protected Sub ButtonDeleteFil e_Click(ByVal sender As Object, ByVal e As DataGridCommand EventArgs) Handles DataGrid1.ItemC ommand
        If Not e.CommandName <> "Delete" Then
        Return

        Dim sessionObject As Object = Session("docDat aTable")

        If Not (sessionObject Is Nothing) Then
        Dim idValue As String = e.Item.Cells(0) .Text

        Dim doc As DataTable = CType(sessionOb ject, DataTable)
        Dim sqlFilter As String = "Document = '" + idValue + "'"
        Dim filteredArray As DataRow() = doc.Select(sqlF ilter)
        If filteredArray.L ength > 0 Then
        Dim filteredRow As DataRow = filteredArray(0 )
        doc.Rows.Remove (filteredRow)
        End If
        End If
        End If

        End Sub
        [/code]
        But this doesnt even get to the if stmt and i dont know why VisualStudio is says "unused local variable" for each and every varaible used here.
        Last edited by Frinavale; Sep 7 '07, 01:06 PM. Reason: Added [code] tags to make more legible

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Do you have more then one function handling that event?

          Comment

          • sspost
            New Member
            • Sep 2007
            • 15

            #6
            No I dont
            Now I am getting this :
            at the following line:

            doc.Rows.Remove (filteredRow)

            it gives me exception: System.IndexOut OfRangeExceptio n - The given DataRow is not in the current DataRowCollecti on. , even though the search row is the primary key one........does anyone has any idea why its giving it? even though the comparing row is the correct one.....

            Comment

            Working...