我有一个带有多个数据绑定源的DataGridView的窗体。表单正确加载适当的数据
如果我在新行选择上刷新表单,它将返回到表单的顶部。我看到有一个DataGridView.Scroll事件。我需要做什么?还有什么我需要改变才能使我的表格再看起来漂亮吗?
THX!
修改
以下是运行到该点的代码。没有任何其他方法可以处理绘制/重绘表单的方式,并且DataGridView1.scroll上没有事件处理
Imports datagridviewautofilter
Imports System.Data.SqlClient
Public Class AssignShipDateForm
'sql datastream connection variables
Dim Conn As New System.Data.SqlClient.SqlConnection 'sql server datastream connection
Dim Cmd As New System.Data.SqlClient.SqlCommand 'sql command vars
Dim SqlQuery As String 'string var used to hold various SQL queries
Dim SqlInsertQuery As String 'sql insert query for data adapter
Dim SqlUpdateQuery As String 'sql update query for data adapter
Dim SqlDeleteQuery As String 'sql delete query for data adapter
Dim data As System.Data.SqlClient.SqlDataReader 'datareader object variable
Dim ConnString As String
Dim da As SqlDataAdapter
'MasterVars table variables
Public Vers As String = Nothing 'tally sheet version
Public Testing As Boolean = False 'tf testing
Public SendMailAcct As String = Nothing 'send email account for automated emails
Public SendMailPW As String = Nothing 'password for email account
Public SMTPUseSSL As Boolean = True ' SMTP SSL
Public SMTPAuthenticate As Integer = 0 'Authenticate SMTP
Public SMTPServer As String = Nothing 'Email Server
Public SendUsing As Integer = 0 'Send Using
Public SMTPServerPort As Integer = 0 'SMTP Email Server Port
Public TestingEmail As String = Nothing 'email account that all emails should be directed to when version is testing
Public PrePricingEmail As String = Nothing 'email account that all prod prepricing emails should be directed to
Public ImperataEmail As String = Nothing 'email account that all prod Imperata emails should be directed to (for permits etc)
Public DirectShipEmail1 As String = Nothing 'email account that all prod direct ship orders should be directed to
Public DirectShipEmail2 As String = Nothing 'email account that all prod direct ship orders should be directed to
Public DirectShipEmail3 As String = Nothing 'email account that all prod direct ship orders should be directed to
Public DirectShipEmail4 As String = Nothing 'email account that all prod direct ship orders should be directed to
'user security variables
Public EMailAddress As String = Nothing 'User's email address
Public ThePersonsName As String = Nothing 'User's name
Public Logistics As String = Nothing 'logistics module access level
'close form with changes pending variables
Dim CloseForm As New Shared_Code.SharedCode
Dim ShouldICloseForm As String = Nothing 'determiniation if the form should be closed or not
'error reporting variables
Dim ErrorReporting As New ErrorReporting.ErrorReporting
Public Shared ErrCode As String = Nothing 'error code
Public Shared ErrVar As String = Nothing 'variable that caused error
Public Shared ErrorCounter As Integer = 0 'error Counter
Public Shared ErrAct As Boolean = False 't/f fatal error
'INITIALIZE FORM
Dim FirstPass As Boolean = True 't/f switch to determine if form initialize is on a first pass or subsequent pass
'CELL VALUE CHANGED
Dim RowUpdated As Boolean = False 't/f flag to set if the row has been updated
Dim SelectedShipDate As String = Nothing 'selected ship date
Dim SelectedShipDateCounter As Integer = 0 'selected ship date PKEY for TempTable
Dim NumberOfTrucksAvailable As Integer = 0 'number of trucks available for selected Ship Date
Dim DefaultPickDateCounter As Integer = 0 'counter (PKEY) for default pick date
Dim DefaultPickDate As String = Nothing 'default pick date derived from PKEY
Dim i As Integer = 0 'counter variable for adding number of trucks available from MasterBusinessDatesTable
'SAVE BUTTON
Dim vbYN As String = Nothing 'vbyn for msgbox
Dim PickID As String = Nothing 'Pkey for logistics records
Dim DTS As DateTime = Nothing 'DTS for updates
'********************************************************************************
'* *
'*CBB00: FORM LOAD *
'* *
'********************************************************************************
Public Sub New(ByRef vers As String, ByRef testing As Boolean, ByRef SendMailAcct As String, ByRef SendMailPW As String, ByVal SMTPUseSSL As Boolean, ByVal SMTPAuthenticate As Integer, ByVal SMTPServer As String, ByVal SendUsing As Integer, ByVal SMTPServerPort As Integer, ByRef TestingEmail As String, ByRef PrePricingEmail As String, ByRef ImperataEmail As String, ByRef DirectShipEmail1 As String, ByRef DirectShipEmail2 As String, ByRef DirectShipEmail3 As String, ByRef DirectShipEmail4 As String, ByRef EMailAddress As String, ByRef ThePersonsName As String, ByRef Logistics As String)
InitializeComponent()
Me.Vers = vers
Me.Testing = testing
Me.SendMailAcct = SendMailAcct
Me.SendMailPW = SendMailPW
Me.SMTPUseSSL = SMTPUseSSL
Me.SMTPAuthenticate = SMTPAuthenticate
Me.SMTPServer = SMTPServer
Me.SendUsing = SendUsing
Me.SMTPServerPort = SMTPServerPort
Me.TestingEmail = TestingEmail
Me.PrePricingEmail = PrePricingEmail
Me.ImperataEmail = ImperataEmail
Me.DirectShipEmail1 = DirectShipEmail1
Me.DirectShipEmail2 = DirectShipEmail2
Me.DirectShipEmail3 = DirectShipEmail3
Me.DirectShipEmail4 = DirectShipEmail4
Me.EMailAddress = EMailAddress
Me.ThePersonsName = ThePersonsName
Me.Logistics = Logistics
'set connection string and form for prod vs. test
If testing = True Then
ConnString = "data Source=SQL01;Initial Catalog=TallySheetTest;Integrated Security=SSPI;"
Me.Text = "Assign Ship Dates " & vers & " TESTING"
Else
ConnString = "data Source=SQL01;Initial Catalog=TallySheet;Integrated Security=SSPI;"
Me.Text = "Assign Ship Dates " & vers
End If
For Each col As DataGridViewColumn In DataGridView1.Columns
col.HeaderCell = New DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell)
Next
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
Call FormInitialize()
End Sub
'********************************************************************************
'* *
'*CBC00: FORM INITIALIZE *
'* *
'********************************************************************************
Public Sub FormInitialize()
'clear Dataset
Me.TallySheetDataSet.Clear()
If FirstPass = True Then
'Do nothing
Else
'????????
End If
FirstPass = False
'Set buttons
Select Case Logistics
Case "I"
Button1.Enabled = False
Button2.Enabled = False
Case Else
Button1.Enabled = True
Button2.Enabled = False
End Select
'TODO: This line of code loads data into the 'TallySheetDataSet.TruckTypeTable' table. You can move, or remove it, as needed.
Me.TruckTypeTableTableAdapter.Fill(Me.TallySheetDataSet.TruckTypeTable)
'SPCBC0001 Check if TempTable1 is empty
Try
Using Conn = New SqlConnection(ConnString)
Dim cmd As New SqlCommand
cmd.Connection = Conn
cmd.CommandText = "SPCBC0001"
cmd.CommandType = CommandType.StoredProcedure
Conn.Open()
data = cmd.ExecuteReader()
data.Read()
If data.HasRows = True Then
ErrCode = "CBC00-03"
ErrVar = "TempTable1"
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
vbYN = MsgBox("Would you like to clear temporary tables?", vbQuestion + vbYesNo + vbSystemModal, "")
If vbYN = vbYes Then
Call ClearTempTables()
Else
Exit Sub
End If
End If
End Using
Catch ex As SqlException
ErrCode = "CBC00-04"
ErrVar = "SPCBC0001;" & vbCrLf & vbCrLf & ex.Message
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
If ErrAct = True Then
Exit Sub
End If
Finally
Conn.Close()
Conn.Dispose()
End Try
'SPCBC0002 - Check if TempTable2 is empty
Try
Using Conn = New SqlConnection(ConnString)
Dim cmd As New SqlCommand
cmd.Connection = Conn
cmd.CommandText = "SPCBC0002"
cmd.CommandType = CommandType.StoredProcedure
Conn.Open()
data = cmd.ExecuteReader()
data.Read()
If data.HasRows = True Then
ErrCode = "CBC00-06"
ErrVar = "TempTable2"
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
vbYN = MsgBox("Would you like to clear temporary tables?", vbQuestion + vbYesNo + vbSystemModal, "")
If vbYN = vbYes Then
Call ClearTempTables()
Else
Exit Sub
End If
End If
End Using
Catch ex As SqlException
ErrCode = "CBC00-07"
ErrVar = "SPCBC0002" & vbCrLf & vbCrLf & ex.Message
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
If ErrAct = True Then
Exit Sub
End If
Finally
Conn.Close()
Conn.Dispose()
End Try
'SPCBC0003 - Fill TempTable1 from MasterOrderTable, Customer, MasterLog
Try
Using Conn = New SqlConnection(ConnString)
Dim cmd As New SqlCommand
cmd.Connection = Conn
cmd.CommandText = "SPCBC0003"
cmd.CommandType = CommandType.StoredProcedure
Conn.Open()
cmd.ExecuteNonQuery()
End Using
Catch ex As Exception
ErrCode = "CBC00-01"
ErrVar = "SPCBC0003" & vbCrLf & vbCrLf & ex.Message
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
If ErrAct = True Then
Exit Sub
End If
Finally
Conn.Close()
Conn.Dispose()
End Try
'SPCBC0004 - Fill DataSet TempTable1 from TempTable1
Try
Using Conn = New SqlConnection(ConnString)
Dim cmd As New SqlCommand("SPCBC0004", Conn)
cmd.CommandType = CommandType.StoredProcedure
Dim da As New SqlDataAdapter(cmd)
da.Fill(Me.TallySheetDataSet.TempTable1MasterOrders)
End Using
Catch ex As SqlException
ErrCode = "CBC00-05"
ErrVar = "SPCBC0004" & vbCrLf & vbCrLf & ex.Message
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
If ErrAct = True Then
Exit Sub
End If
Finally
Conn.Close()
Conn.Dispose()
End Try
'handling if no rows exist
If TallySheetDataSet.TempTable1MasterOrders.Rows.Count = 0 Then
MsgBox("No open orders") '<-------------------------------- add error reporting?
Exit Sub
End If
'SPCBC0005 - Fill TempTable2 from MasterBusinessDatesTable
Try
Using Conn = New SqlConnection(ConnString)
Dim cmd As New SqlCommand
cmd.Connection = Conn
cmd.CommandText = "SPCBC0005"
cmd.CommandType = CommandType.StoredProcedure
Conn.Open()
cmd.ExecuteNonQuery()
End Using
Catch ex As SqlException
ErrCode = "CBC00-02"
ErrVar = "SPCBC0005" & vbCrLf & vbCrLf & ex.Message
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
If ErrAct = True Then
Exit Sub
End If
Finally
Conn.Close()
Conn.Dispose()
End Try
'SPCBC0006 - Fill DataSet TempTable2 from TempTable2
Try
Using Conn = New SqlConnection(ConnString)
Dim cmd As New SqlCommand("SPCBC0006", Conn)
cmd.CommandType = CommandType.StoredProcedure
Dim da As New SqlDataAdapter(cmd)
da.Fill(Me.TallySheetDataSet.TempTable2BusinessDates)
End Using
Catch ex As SqlException
ErrCode = "CBC00-08"
ErrVar = "SPCBC0006" & vbCrLf & vbCrLf & ex.Message
ErrorCounter = ErrorCounter + 1
ErrAct = ErrorReporting.YouBrokeMyProgram("", ErrCode, ErrVar, ErrorCounter, Testing, Vers, TestingEmail, SendMailAcct, SendMailPW, EMailAddress, SMTPUseSSL, SMTPAuthenticate, SMTPServer, SendUsing, SMTPServerPort)
If ErrAct = True Then
Exit Sub
End If
Finally
Conn.Close()
Conn.Dispose()
End Try
'set ALL columns read only property to true
DataGridView1.Columns(0).ReadOnly = True
DataGridView1.Columns(1).ReadOnly = True
DataGridView1.Columns(2).ReadOnly = True
DataGridView1.Columns(3).ReadOnly = True
DataGridView1.Columns(4).ReadOnly = True
DataGridView1.Columns(5).ReadOnly = True
DataGridView1.Columns(6).ReadOnly = True
DataGridView1.Columns(7).ReadOnly = True
DataGridView1.Columns(8).ReadOnly = True
DataGridView1.Columns(9).ReadOnly = True
DataGridView1.Columns(10).ReadOnly = True
DataGridView1.Columns(11).ReadOnly = True
DataGridView1.Columns(12).ReadOnly = True
DataGridView1.Columns(13).ReadOnly = True
DataGridView1.Columns(14).ReadOnly = True
DataGridView1.Columns(15).ReadOnly = True
DataGridView1.Columns(16).ReadOnly = True
DataGridView1.Columns(17).ReadOnly = True
DataGridView1.Columns(18).ReadOnly = True
DataGridView1.Columns(19).ReadOnly = True
DataGridView1.Columns(20).ReadOnly = True
DataGridView1.Columns(21).ReadOnly = True
DataGridView1.Columns(22).ReadOnly = True
DataGridView1.Columns(23).ReadOnly = True
'Refresh form
Me.Refresh()
DataGridView1.Refresh()
End Sub
修改
我有一个DataGridView1.DataError的空处理程序。我添加了一个消息框,“发货日期”为列中的每个值抛出一个错误,然后当您滚动时,它会为出现的每个新“发货日期”单元格抛出一个错误。错误是在不接受空值的列中显示空值。空值是尝试禁止显示值“1900-01-01”。
答案 0 :(得分:0)
显示错误是由于在不接受空值的列中显示空值而被抑制的错误消息引起的。空值是为了抑制值&#39; 1900-01-01&#39;从显示。