我创建了一个应用程序,该函数是来自sql的下载文件。为此,我添加了一个带按钮和checkboxcolumn列的数据网格。
带按钮的功能正常但我无法按所选行列下载文件。
你能帮我解决这个问题。
这是我的代码:
Imports System.Data
Imports System.IO
Imports System.Windows.Threading
Public Class Activity
Public Setfile As Integer
Public drv As DataRowView
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
txtId.Content = Log_Name
txtName.Content = EmpNombre & " " & EmpApPat & " " & EmpApMat
txtSite.Content = Log_Site
txtType.Content = Log_Type
'load files
GetFilesFromDatabase()
'reset idle counter
ResetAutoIdleTimeCounter()
End Sub
Private Sub btnUpFile_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnUpFile.Click
If Log_Type = "ADMIN" Then
Dim NUpl As New file_upload()
NUpl.ShowDialog()
Else
lblstatus.Content = "Access Denied."
lblstatus.Foreground = New SolidColorBrush(Colors.Red)
lblstatus.Opacity = 1
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub GetFilesFromDatabase()
Try
querySQL = "SELECT file_Id as ID, file_title as FILENAME, file_mimetype as EXTENSION, file_rev as REVISION, file_upl as [DATE UPLOADED], file_pub as [UPL BY] FROM tbl_file"
sqlDS = New DataSet
dgFiles.Columns.Clear()
sqlConn.Open()
sqlCmd = New SqlClient.SqlCommand(querySQL, sqlConn)
sqlCmd.CommandType = CommandType.Text
sqlAdap = New SqlClient.SqlDataAdapter(sqlCmd)
sqlAdap.Fill(sqlDS, "FileStore")
sqlConn.Close()
dgFiles.DataContext = sqlDS '.Tables("FileStore").DefaultView
Dim dgCheckColum As New DataGridCheckBoxColumn
dgCheckColum.Header = "SEL"
dgCheckColum.DisplayIndex = 0
Dim dgDownColumn As New DataGridTemplateColumn
dgDownColumn.Header = "FILE"
dgDownColumn.DisplayIndex = 1
Dim factory As New FrameworkElementFactory(GetType(Button))
factory.SetValue(Button.ContentProperty, "Download")
factory.SetValue(Button.WidthProperty, CDbl(75))
factory.SetValue(Button.HeightProperty, CDbl(35))
factory.AddHandler(Button.ClickEvent, New RoutedEventHandler(AddressOf button_Click))
Dim cellTemplate As New DataTemplate()
cellTemplate.VisualTree = factory
dgDownColumn.CellTemplate = cellTemplate
dgFiles.Columns.Add(dgCheckColum)
dgFiles.Columns.Add(dgDownColumn)
Catch ex As Exception
MessageBox.Show(ex.ToString())
MessageBox.Show("Could not load the File")
End Try
End Sub
Private Sub button_Click(sender As Object, e As System.Windows.RoutedEventArgs)
Try
Dim SelId As Integer
drv = dgFiles.CurrentCell.Item
SelId = drv.Row(0).ToString()
DownloadFile(SelId, drv.Row(1).ToString(), drv.Row(2).ToString())
Catch ex As Exception
MessageBox.Show(ex.ToString())
MessageBox.Show("Could not load the File")
End Try
End Sub
Private Sub DownloadFile(ByVal Id As Integer, ByVal filename As String, ByVal ext As String)
Try
querySQL = "SELECT file_bin FROM tbl_file WHERE file_id=@id"
sqlConn.Open()
sqlCmd = New SqlClient.SqlCommand(querySQL, sqlConn)
sqlCmd.CommandType = CommandType.Text
sqlCmd.Parameters.AddWithValue("@id", Id)
'get image data from db
fileData = DirectCast(sqlCmd.ExecuteScalar(), Byte())
Dim Gig As Long = 1073741824
For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives
If drive.DriveType = IO.DriveType.Removable AndAlso drive.IsReady AndAlso drive.AvailableFreeSpace >= 1 * Gig Then
Dim DriveLetter As String = drive.Name
Dim PathToUSBDrive = DriveLetter & IO.Path.GetFileName(filename.ToString.Trim & ext.ToString.Trim)
Using fs As New FileStream(PathToUSBDrive, FileMode.Append)
fs.Write(fileData, 0, fileData.Length)
FileOK = True
lblstatus.Content = "Done."
lblstatus.Foreground = New SolidColorBrush(Colors.LawnGreen)
lblstatus.Opacity = 1
'Set image variable value using memory stream.
fs.Close()
FileOK = True
End Using
Else
lblstatus.Content = "Error: Check if the memory is not full or damaged."
lblstatus.Foreground = New SolidColorBrush(Colors.Red)
lblstatus.Opacity = 1
End If
Next
sqlConn.Close()
If FileOK = True Then
'fill the activity table
SaveAct(Log_Id, drv.Row(0), True, "The user " & Log_Id & " have downloaded the file " & filename & "." & ext & ". Rev " & drv.Row(3).ToString())
End If
'Clear row values
drv = Nothing
Catch ex As Exception
MessageBox.Show(ex.ToString())
MessageBox.Show("Could not load the File")
Finally
sqlConn.Close()
End Try
End Sub
Private Sub LoadAct(ByVal Id As Integer)
Try
sqlDS = New DataSet
sqlConn.Open()
sqlCmd = New SqlClient.SqlCommand("SP_CHKACT", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@Id", Id)
sqlAdap = New SqlClient.SqlDataAdapter(sqlCmd)
sqlAdap.Fill(sqlDS, "Activity")
sqlConn.Close()
dgHist.DataContext = sqlDS '.Tables("FileStore").DefaultView
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
sqlConn.Close()
End Try
End Sub
Private Sub Window_Activated(sender As System.Object, e As System.EventArgs) Handles MyBase.Activated
LoadAct(Log_Name)
End Sub
Private Sub Window_PreviewKeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles MyBase.PreviewKeyDown
ResetAutoIdleTimeCounter()
End Sub
Private Sub Window_PreviewMouseDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs) Handles MyBase.PreviewMouseDown
ResetAutoIdleTimeCounter()
End Sub
Private Sub ResetAutoIdleTimeCounter()
' Or DispatcherPriority.SystemIdle
Dim timer = New DispatcherTimer(TimeSpan.FromMinutes(1),
DispatcherPriority.ApplicationIdle,
New Action(Sub()
For intCounter As Integer = Application.Current.Windows.Count - 1 To 0 Step -1
If Application.Current.Windows(intCounter) IsNot Application.Current.MainWindow Then
Application.Current.Windows(intCounter).Close()
End If
Next
End Sub),
Application.Current.Dispatcher)
End Sub
Private Sub btnOk_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnOk.Click
'load files
GetFilesFromDatabase()
'reset idle counter
ResetAutoIdleTimeCounter()
End Sub
答案 0 :(得分:0)
所以当你说你只想下载所选文件时,你是说当你点击button_Click
时,你想要为你在数据网格中选择的任何行调用你的下载功能吗?