本文主要是介绍成績查詢(vb.net2003 from listview datagrid),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
界面:
代碼:
Imports System.Data.SqlClient
Public Class Form1 Class Form1
Inherits System.Windows.Forms.Form
Windows Form 設計工具產生的程式碼#Region " Windows Form 設計工具產生的程式碼 "
Public Sub New()Sub New()
MyBase.New()
'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()
'在 InitializeComponent() 呼叫之後加入所有的初始設定
End Sub
'Form 覆寫 Dispose 以清除元件清單。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'為 Windows Form 設計工具的必要項
Private components As System.ComponentModel.IContainer
'注意: 以下為 Windows Form 設計工具所需的程序
'您可以使用 Windows Form 設計工具進行修改。
'請勿使用程式碼編輯器來修改這些程序。
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents QueryTxt As System.Windows.Forms.TextBox
Friend WithEvents QueryBtn As System.Windows.Forms.Button
Friend WithEvents AvgRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents XHRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents ScrRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents StuRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents StuInfoLV As System.Windows.Forms.ListView
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.AvgRadBtn = New System.Windows.Forms.RadioButton
Me.XHRadBtn = New System.Windows.Forms.RadioButton
Me.GroupBox2 = New System.Windows.Forms.GroupBox
Me.ScrRadBtn = New System.Windows.Forms.RadioButton
Me.StuRadBtn = New System.Windows.Forms.RadioButton
Me.QueryTxt = New System.Windows.Forms.TextBox
Me.QueryBtn = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.StuInfoLV = New System.Windows.Forms.ListView
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.AvgRadBtn)
Me.GroupBox1.Controls.Add(Me.XHRadBtn)
Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(224, 64)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "查詢條件"
'
'AvgRadBtn
'
Me.AvgRadBtn.Location = New System.Drawing.Point(104, 16)
Me.AvgRadBtn.Name = "AvgRadBtn"
Me.AvgRadBtn.Size = New System.Drawing.Size(72, 24)
Me.AvgRadBtn.TabIndex = 2
Me.AvgRadBtn.Text = "平均成績"
'
'XHRadBtn
'
Me.XHRadBtn.Checked = True
Me.XHRadBtn.Location = New System.Drawing.Point(24, 16)
Me.XHRadBtn.Name = "XHRadBtn"
Me.XHRadBtn.Size = New System.Drawing.Size(72, 24)
Me.XHRadBtn.TabIndex = 1
Me.XHRadBtn.TabStop = True
Me.XHRadBtn.Text = "學號"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.ScrRadBtn)
Me.GroupBox2.Controls.Add(Me.StuRadBtn)
Me.GroupBox2.Location = New System.Drawing.Point(240, 8)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(208, 64)
Me.GroupBox2.TabIndex = 1
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "返回結果"
'
'ScrRadBtn
'
Me.ScrRadBtn.Location = New System.Drawing.Point(112, 16)
Me.ScrRadBtn.Name = "ScrRadBtn"
Me.ScrRadBtn.Size = New System.Drawing.Size(72, 24)
Me.ScrRadBtn.TabIndex = 1
Me.ScrRadBtn.Text = "成績信息"
'
'StuRadBtn
'
Me.StuRadBtn.Checked = True
Me.StuRadBtn.Location = New System.Drawing.Point(24, 16)
Me.StuRadBtn.Name = "StuRadBtn"
Me.StuRadBtn.Size = New System.Drawing.Size(72, 24)
Me.StuRadBtn.TabIndex = 0
Me.StuRadBtn.TabStop = True
Me.StuRadBtn.Text = "學生信息"
'
'QueryTxt
'
Me.QueryTxt.Location = New System.Drawing.Point(8, 112)
Me.QueryTxt.Name = "QueryTxt"
Me.QueryTxt.Size = New System.Drawing.Size(320, 22)
Me.QueryTxt.TabIndex = 2
Me.QueryTxt.Text = ""
'
'QueryBtn
'
Me.QueryBtn.Location = New System.Drawing.Point(344, 112)
Me.QueryBtn.Name = "QueryBtn"
Me.QueryBtn.TabIndex = 3
Me.QueryBtn.Text = "查詢"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 80)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(100, 24)
Me.Label1.TabIndex = 4
Me.Label1.Text = "請輸入學號"
'
'DataGrid1
'
Me.DataGrid1.BackgroundColor = System.Drawing.Color.Lavender
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 152)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(440, 128)
Me.DataGrid1.TabIndex = 5
'
'StuInfoLV
'
Me.StuInfoLV.Location = New System.Drawing.Point(8, 176)
Me.StuInfoLV.Name = "StuInfoLV"
Me.StuInfoLV.Size = New System.Drawing.Size(440, 97)
Me.StuInfoLV.TabIndex = 6
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.ClientSize = New System.Drawing.Size(472, 293)
Me.Controls.Add(Me.StuInfoLV)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.QueryBtn)
Me.Controls.Add(Me.QueryTxt)
Me.Controls.Add(Me.GroupBox2)
Me.Controls.Add(Me.GroupBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub GetStuInfoById()Sub GetStuInfoById()
StuInfoLV.Columns.Clear()
StuInfoLV.Items.Clear()
StuInfoLV.View = View.Details
StuInfoLV.HeaderStyle = ColumnHeaderStyle.Clickable
StuInfoLV.MultiSelect = True
StuInfoLV.FullRowSelect = True
StuInfoLV.GridLines = True
StuInfoLV.Columns.Add("學號", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("姓名", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("年齡", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("性別", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("年級", 50, HorizontalAlignment.Left)
Dim Query As String = "select * from student where id=" + QueryTxt.Text
Dim consql As SqlConnection = New SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=;")
Dim myCommand As SqlCommand = New SqlCommand(Query, consql)
myCommand.Connection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
StuInfoLV.BeginUpdate()
While myReader.Read()
Dim id As String = myReader.GetDecimal(0).ToString()
Dim age As String = myReader.GetDecimal(2).ToString()
Dim sex As String = myReader.GetDecimal(3).ToString()
Dim grade As String = myReader.GetString(4).ToString()
Dim name As String = myReader.GetString(1).ToString()
If sex = "1" Then
sex = "男"
Else
sex = "女"
End If
Dim listSubItem As String() = {id, name, age, sex, grade}
Dim li As ListViewItem = New ListViewItem(listSubItem)
StuInfoLV.Items.Add(li)
End While
myReader.Close()
myCommand.Connection.Close()
StuInfoLV.EndUpdate()
End Sub
Private Sub GetSteudentInfoScoreByID()Sub GetSteudentInfoScoreByID()
Dim consql As SqlConnection = New SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=;")
'Dim consql As SqlConnection = New SqlConnection("Data Source=(local);Initial Catalog=test;user id=sa;password=;")
Dim Query As String = "select a.id,a.name,b.course,b.score,b.type from student a left join score b on a.id=b.id where a.id=" + QueryTxt.Text
Dim myadpter As SqlDataAdapter = New SqlDataAdapter(Query, consql)
Dim myTable As DataTable = New DataTable
myadpter.Fill(myTable)
myTable.Columns(0).ColumnName = "學號"
myTable.Columns(1).ColumnName = "姓名"
myTable.Columns(2).ColumnName = "課程"
myTable.Columns(3).ColumnName = "成績"
myTable.Columns(4).ColumnName = "類型"
Dim i As Int16
For i = 0 To myTable.Rows.Count() - 1
Dim type As String = CType(myTable.Rows(i).Item(4), String)
If type = "0" Then
myTable.Rows(i).Item(4) = "主修"
Else
myTable.Rows(i).Item(4) = "選修"
End If
Next
DataGrid1.DataSource = myTable
End Sub
Private Sub GetStuInfoByScore()Sub GetStuInfoByScore()
Dim ds As DataSet = New DataSet
Dim consql As SqlConnection = New SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=;")
Dim Query As String = "select a.id,a.name,a.grade,B.cj from student a,(select id,avg(score) cj from score group by id) B where a.id=B.id"
Dim myadpter As SqlDataAdapter = New SqlDataAdapter(Query, consql)
myadpter.Fill(ds, "student")
Dim myTable As DataTable = ds.Tables("student")
myTable.Columns(0).ColumnName = "學號"
myTable.Columns(1).ColumnName = "姓名"
myTable.Columns(2).ColumnName = "年級"
myTable.Columns(3).ColumnName = "平均成績"
Dim myDataView As DataView = New DataView
With myDataView
.Table = ds.Tables("student")
.RowStateFilter = DataViewRowState.CurrentRows
.Sort = "平均成績,年級"
.RowFilter = "平均成績>" + QueryTxt.Text
End With
DataGrid1.DataSource = myDataView
End Sub
Private Sub QueryBtn_Click()Sub QueryBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QueryBtn.Click
'根據學號查詢學生信息
If XHRadBtn.Checked And StuRadBtn.Checked Then
StuInfoLV.Visible = True
DataGrid1.Visible = False
GetStuInfoById()
Return
End If
'根據學號查詢學生成績
If XHRadBtn.Checked And ScrRadBtn.Checked Then
StuInfoLV.Visible = False
DataGrid1.Visible = True
GetSteudentInfoScoreByID()
Return
End If
'根據平均成績查詢學生信息
If AvgRadBtn.Checked Then
StuInfoLV.Visible = False
DataGrid1.Visible = True
GetStuInfoByScore()
Return
End If
End Sub
End Class
Public Class Form1 Class Form1
Inherits System.Windows.Forms.Form
Windows Form 設計工具產生的程式碼#Region " Windows Form 設計工具產生的程式碼 "
Public Sub New()Sub New()
MyBase.New()
'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()
'在 InitializeComponent() 呼叫之後加入所有的初始設定
End Sub
'Form 覆寫 Dispose 以清除元件清單。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'為 Windows Form 設計工具的必要項
Private components As System.ComponentModel.IContainer
'注意: 以下為 Windows Form 設計工具所需的程序
'您可以使用 Windows Form 設計工具進行修改。
'請勿使用程式碼編輯器來修改這些程序。
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents QueryTxt As System.Windows.Forms.TextBox
Friend WithEvents QueryBtn As System.Windows.Forms.Button
Friend WithEvents AvgRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents XHRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents ScrRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents StuRadBtn As System.Windows.Forms.RadioButton
Friend WithEvents StuInfoLV As System.Windows.Forms.ListView
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.AvgRadBtn = New System.Windows.Forms.RadioButton
Me.XHRadBtn = New System.Windows.Forms.RadioButton
Me.GroupBox2 = New System.Windows.Forms.GroupBox
Me.ScrRadBtn = New System.Windows.Forms.RadioButton
Me.StuRadBtn = New System.Windows.Forms.RadioButton
Me.QueryTxt = New System.Windows.Forms.TextBox
Me.QueryBtn = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.StuInfoLV = New System.Windows.Forms.ListView
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.AvgRadBtn)
Me.GroupBox1.Controls.Add(Me.XHRadBtn)
Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(224, 64)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "查詢條件"
'
'AvgRadBtn
'
Me.AvgRadBtn.Location = New System.Drawing.Point(104, 16)
Me.AvgRadBtn.Name = "AvgRadBtn"
Me.AvgRadBtn.Size = New System.Drawing.Size(72, 24)
Me.AvgRadBtn.TabIndex = 2
Me.AvgRadBtn.Text = "平均成績"
'
'XHRadBtn
'
Me.XHRadBtn.Checked = True
Me.XHRadBtn.Location = New System.Drawing.Point(24, 16)
Me.XHRadBtn.Name = "XHRadBtn"
Me.XHRadBtn.Size = New System.Drawing.Size(72, 24)
Me.XHRadBtn.TabIndex = 1
Me.XHRadBtn.TabStop = True
Me.XHRadBtn.Text = "學號"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.ScrRadBtn)
Me.GroupBox2.Controls.Add(Me.StuRadBtn)
Me.GroupBox2.Location = New System.Drawing.Point(240, 8)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(208, 64)
Me.GroupBox2.TabIndex = 1
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "返回結果"
'
'ScrRadBtn
'
Me.ScrRadBtn.Location = New System.Drawing.Point(112, 16)
Me.ScrRadBtn.Name = "ScrRadBtn"
Me.ScrRadBtn.Size = New System.Drawing.Size(72, 24)
Me.ScrRadBtn.TabIndex = 1
Me.ScrRadBtn.Text = "成績信息"
'
'StuRadBtn
'
Me.StuRadBtn.Checked = True
Me.StuRadBtn.Location = New System.Drawing.Point(24, 16)
Me.StuRadBtn.Name = "StuRadBtn"
Me.StuRadBtn.Size = New System.Drawing.Size(72, 24)
Me.StuRadBtn.TabIndex = 0
Me.StuRadBtn.TabStop = True
Me.StuRadBtn.Text = "學生信息"
'
'QueryTxt
'
Me.QueryTxt.Location = New System.Drawing.Point(8, 112)
Me.QueryTxt.Name = "QueryTxt"
Me.QueryTxt.Size = New System.Drawing.Size(320, 22)
Me.QueryTxt.TabIndex = 2
Me.QueryTxt.Text = ""
'
'QueryBtn
'
Me.QueryBtn.Location = New System.Drawing.Point(344, 112)
Me.QueryBtn.Name = "QueryBtn"
Me.QueryBtn.TabIndex = 3
Me.QueryBtn.Text = "查詢"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 80)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(100, 24)
Me.Label1.TabIndex = 4
Me.Label1.Text = "請輸入學號"
'
'DataGrid1
'
Me.DataGrid1.BackgroundColor = System.Drawing.Color.Lavender
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 152)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(440, 128)
Me.DataGrid1.TabIndex = 5
'
'StuInfoLV
'
Me.StuInfoLV.Location = New System.Drawing.Point(8, 176)
Me.StuInfoLV.Name = "StuInfoLV"
Me.StuInfoLV.Size = New System.Drawing.Size(440, 97)
Me.StuInfoLV.TabIndex = 6
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.ClientSize = New System.Drawing.Size(472, 293)
Me.Controls.Add(Me.StuInfoLV)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.QueryBtn)
Me.Controls.Add(Me.QueryTxt)
Me.Controls.Add(Me.GroupBox2)
Me.Controls.Add(Me.GroupBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub GetStuInfoById()Sub GetStuInfoById()
StuInfoLV.Columns.Clear()
StuInfoLV.Items.Clear()
StuInfoLV.View = View.Details
StuInfoLV.HeaderStyle = ColumnHeaderStyle.Clickable
StuInfoLV.MultiSelect = True
StuInfoLV.FullRowSelect = True
StuInfoLV.GridLines = True
StuInfoLV.Columns.Add("學號", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("姓名", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("年齡", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("性別", 50, HorizontalAlignment.Left)
StuInfoLV.Columns.Add("年級", 50, HorizontalAlignment.Left)
Dim Query As String = "select * from student where id=" + QueryTxt.Text
Dim consql As SqlConnection = New SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=;")
Dim myCommand As SqlCommand = New SqlCommand(Query, consql)
myCommand.Connection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
StuInfoLV.BeginUpdate()
While myReader.Read()
Dim id As String = myReader.GetDecimal(0).ToString()
Dim age As String = myReader.GetDecimal(2).ToString()
Dim sex As String = myReader.GetDecimal(3).ToString()
Dim grade As String = myReader.GetString(4).ToString()
Dim name As String = myReader.GetString(1).ToString()
If sex = "1" Then
sex = "男"
Else
sex = "女"
End If
Dim listSubItem As String() = {id, name, age, sex, grade}
Dim li As ListViewItem = New ListViewItem(listSubItem)
StuInfoLV.Items.Add(li)
End While
myReader.Close()
myCommand.Connection.Close()
StuInfoLV.EndUpdate()
End Sub
Private Sub GetSteudentInfoScoreByID()Sub GetSteudentInfoScoreByID()
Dim consql As SqlConnection = New SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=;")
'Dim consql As SqlConnection = New SqlConnection("Data Source=(local);Initial Catalog=test;user id=sa;password=;")
Dim Query As String = "select a.id,a.name,b.course,b.score,b.type from student a left join score b on a.id=b.id where a.id=" + QueryTxt.Text
Dim myadpter As SqlDataAdapter = New SqlDataAdapter(Query, consql)
Dim myTable As DataTable = New DataTable
myadpter.Fill(myTable)
myTable.Columns(0).ColumnName = "學號"
myTable.Columns(1).ColumnName = "姓名"
myTable.Columns(2).ColumnName = "課程"
myTable.Columns(3).ColumnName = "成績"
myTable.Columns(4).ColumnName = "類型"
Dim i As Int16
For i = 0 To myTable.Rows.Count() - 1
Dim type As String = CType(myTable.Rows(i).Item(4), String)
If type = "0" Then
myTable.Rows(i).Item(4) = "主修"
Else
myTable.Rows(i).Item(4) = "選修"
End If
Next
DataGrid1.DataSource = myTable
End Sub
Private Sub GetStuInfoByScore()Sub GetStuInfoByScore()
Dim ds As DataSet = New DataSet
Dim consql As SqlConnection = New SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=;")
Dim Query As String = "select a.id,a.name,a.grade,B.cj from student a,(select id,avg(score) cj from score group by id) B where a.id=B.id"
Dim myadpter As SqlDataAdapter = New SqlDataAdapter(Query, consql)
myadpter.Fill(ds, "student")
Dim myTable As DataTable = ds.Tables("student")
myTable.Columns(0).ColumnName = "學號"
myTable.Columns(1).ColumnName = "姓名"
myTable.Columns(2).ColumnName = "年級"
myTable.Columns(3).ColumnName = "平均成績"
Dim myDataView As DataView = New DataView
With myDataView
.Table = ds.Tables("student")
.RowStateFilter = DataViewRowState.CurrentRows
.Sort = "平均成績,年級"
.RowFilter = "平均成績>" + QueryTxt.Text
End With
DataGrid1.DataSource = myDataView
End Sub
Private Sub QueryBtn_Click()Sub QueryBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QueryBtn.Click
'根據學號查詢學生信息
If XHRadBtn.Checked And StuRadBtn.Checked Then
StuInfoLV.Visible = True
DataGrid1.Visible = False
GetStuInfoById()
Return
End If
'根據學號查詢學生成績
If XHRadBtn.Checked And ScrRadBtn.Checked Then
StuInfoLV.Visible = False
DataGrid1.Visible = True
GetSteudentInfoScoreByID()
Return
End If
'根據平均成績查詢學生信息
If AvgRadBtn.Checked Then
StuInfoLV.Visible = False
DataGrid1.Visible = True
GetStuInfoByScore()
Return
End If
End Sub
End Class
代碼下載
數據創建:
CREATE TABLE [ Score ] (
[ ID ] [ nvarchar ] ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ Course ] [ nvarchar ] ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ Score ] [ numeric ] ( 18 , 0 ) NULL ,
[ Type ] [ nvarchar ] ( 1 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL
) ON [ PRIMARY ]
GO
CREATE TABLE [ Student ] (
[ ID ] [ numeric ] ( 18 , 0 ) NULL ,
[ Name ] [ nvarchar ] ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ Age ] [ numeric ] ( 18 , 0 ) NULL ,
[ Sex ] [ numeric ] ( 5 , 0 ) NOT NULL ,
[ Grade ] [ nvarchar ] ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL
) ON [ PRIMARY ]
GO
[ ID ] [ nvarchar ] ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ Course ] [ nvarchar ] ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ Score ] [ numeric ] ( 18 , 0 ) NULL ,
[ Type ] [ nvarchar ] ( 1 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL
) ON [ PRIMARY ]
GO
CREATE TABLE [ Student ] (
[ ID ] [ numeric ] ( 18 , 0 ) NULL ,
[ Name ] [ nvarchar ] ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ Age ] [ numeric ] ( 18 , 0 ) NULL ,
[ Sex ] [ numeric ] ( 5 , 0 ) NOT NULL ,
[ Grade ] [ nvarchar ] ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL
) ON [ PRIMARY ]
GO
这篇关于成績查詢(vb.net2003 from listview datagrid)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!