Solved

How to retrieve a deleted row in GridView

Posted on 2007-12-05
11
804 Views
Last Modified: 2013-11-26
I have bounded a DataGrid to reflect a particular files in a folder. I'm using Visual Studio 2005 with VB coding.

1. Have any idea how to capture the filename when user click the "Delete" Button in GridView.
2. Is it possilbe to include more parameter in DataNavigateUrlFormatString, say ="~/ListFolder.aspx?idir={0}",ifile={2} />

Thanks.


Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        Dim upFolder As String = MapPath("~/DMS/")
        Dim dir As New System.IO.DirectoryInfo(upFolder)

        rptFiles.DataSource = dir.GetFiles()
        rptFiles.DataBind()

        DirTree.DataSource = dir.GetDirectories
        DirTree.DataBind()

    End Sub
        <asp:gridview
        id="dirTree"
       autogeneratecolumns="false"
       GridLines="None"
          Runat="server" HorizontalAlign="Left">
            <Columns>        
          <asp:HyperLinkField
            DataTextField="Name"
            DataNavigateUrlFields="Name"
            DataNavigateUrlFormatString="~/ListFolder.aspx?idir={0}" />
          </Columns>
    </asp:gridview>
0
Comment
Question by:AXISHK
11 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 20417232
Do you want to delete the record from the db and also an associated file from the filesystem?
I ask as I have just done exactly that and could post my code
0
 

Author Comment

by:AXISHK
ID: 20417243
Basically, not touching any Database, only retrieve the files from specifiy path and show on GridView and enable the "Delete" button per button. AFterwards, I willl delete the files from the server.

Thanks.
0
 
LVL 29

Expert Comment

by:QPR
ID: 20417286

This is very over simplified as my code got the doc filename and path from the db based on the DocID for that particular row in the gridview.
You may have to do something like.....
<asp:Button CommandName=<%#eval("DocPath_name")  %> ID="button1" runat="server" Text="Delete" OnClick="button1_Click" />
then in your code behind

Imports System.IO
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
              File.Delete(Server.MapPath(sender.CommandName))
End Sub
0
 
LVL 29

Expert Comment

by:QPR
ID: 20417292
the variable DocPath_name would be populated row by row based on the path to the folder and the filename
OnClick you would pass this value to the code behind which would then pass it to the File.Delete method
0
 

Author Comment

by:AXISHK
ID: 20417426

I tried to pu the control under the "Columns" but doesn't work. It seems that GridView don't support the bind of this control. Any idea. Can you show me how you bind the selected column on GridView ?

Thanks

  <Columns>        
          <asp:HyperLinkField
            DataTextField="Name"
            DataNavigateUrlFields="Name"
            DataNavigateUrlFormatString="~/ListFolder.aspx?idir={0}" />
          </Columns>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:AXISHK
ID: 20417456
Protected Sub dirTree_RowDeleting(ByVal sender As Object, ByVal e As  
    System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles rptFiles.RowDeleting

 End Sub

Bascially the event RowDeleting is called for my previous posted code but I don't know how to retrieve the file name binded to the row of the GridView. Any idea ?

Again, if you think there is a way to get around of this, I could modify my coding on GridView.
tks
0
 
LVL 29

Expert Comment

by:QPR
ID: 20422434
Which bit is the problem?

DataNavigateUrlFormatString="~/ListFolder.aspx?idir={0}"  
this bit?
0
 
LVL 16

Accepted Solution

by:
GreymanMSC earned 500 total points
ID: 20424683
Add DataKeyNames="Name" to the gridview, then access the datakey value in the event.
    Protected Sub dirTree_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles dirTree.RowDeleting

        Dim gv As GridView = CType(sender, GridView)

        Dim TheValueToUse As String = gv.DataKeys(e.RowIndex).Value

        '....

    End Sub
 

        <asp:gridview id="dirTree"

            DataKeyNames="Name"

            autogeneratecolumns="false"

            GridLines="None"

            Runat="server" 

            HorizontalAlign="Left">

            <Columns>        

                <asp:HyperLinkField

                    DataTextField="Name"

                    DataNavigateUrlFields="Name"

                    DataNavigateUrlFormatString="~/ListFolder.aspx?idir={0}" />

                <asp:ButtonField ButtonType=Button CommandName=Delete Text="Delete" />

                

            </Columns>

        </asp:gridview>    

Open in new window

0
 

Author Comment

by:AXISHK
ID: 20424988
thanks. Sound like this is a way to go.

On more things, how to change the "DataNavigateUrlFormatString" per row before bind to GridView using the RowDataBound event. I could access the content of the DataNavigaterUrlField but I want to change the NavigterUrlFormatString dynamically.

Thanks again.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20952985
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now