DataGridView查詢資料

2021-06-16 21:11:28 字數 3139 閱讀 7514

機房收費中查詢記錄,肯定會用到datagridview,下面是我實現的乙個小功能:查詢學生上機記錄。

一、時序圖

二、**

u層

public class frmonlinequery

private sub btnquery_click(sender as object, e as eventargs) handles btnquery.click

dim offlinemodel as new offlinemodel

dim onlinequerybll as new onlinequerybll

dim strresult as string

dim strcardno as string

strcardno = trim(txtcardno.text)

strresult = onlinequerybll.i***it(strcardno)

select case strresult

case "卡號不存在"

msgbox("卡號不存在")

datagridview1.datasource = vbnull

txtcardno.text = ""

case "卡號存在"

dim table as new datatable

table = onlinequerybll.getdata(strcardno)

datagridview1.autogeneratecolumns = false

datagridview1.datasource = table

datagridview1.refresh()

end select

end sub

b層

public class onlinequerybll

public function i***it(byval strcardno as string) as string

dim onlinequerydal as new onlinequerydal

dim offlinemodel as new offlinemodel

if onlinequerydal.i***it(strcardno) = false then

return ("卡號不存在")

else

return "卡號存在"

end if

end function

public function getdata(byval strcardno as string) as datatable

dim onlinequerydal as new onlinequerydal

dim datatable as new datatable

datatable = onlinequerydal.getdata(strcardno)

return datatable

end function

end class

d層

imports model

imports system.data.sqlclient

imports system.data.datatable

public class onlinequerydal

''' ''' 判斷卡號是否存在

'''

'''

'''

'''

public function i***it(byval strcardno as string) as boolean

dim sqlconnection1 as sqlclient.sqlconnection '執行資料庫連線

dim cmd as sqlclient.sqlcommand '使用command物件執行命令並返回

sqlconnection1 = new sqlclient.sqlconnection(con)

cmd = sqlconnection1.createcommand() '建立插入命令

cmd.commandtext = "select * from t_offline where cardno=@cardno "

cmd.commandtype = commandtype.text

cmd.parameters.add(new sqlclient.sqlparameter("@cardno", strcardno))

sqlconnection1.open()

dim reader as sqldatareader

reader = cmd.executereader()

if reader.read() then

return true

else

return false

end if

end function

''' ''' 從下機表中獲取資料

'''

''' 卡號

'''

'''

public function getdata(byval strcardno as string) as datatable

dim adapter as sqldataadapter

adapter = new sqldataadapter("select * from t_offline where cardno= '" & strcardno & "'", con)

dim cmdbuilder as new sqlcommandbuilder(adapter)

dim table as new datatable

table.locale = system.globalization.cultureinfo.invariantculture

adapter.fill(table)

return table

end function

end class

datagridview這個控制項的使用學習了一天,終於弄出來了,雖然用的不是很好但做出來了就很增長信心,以後許多重複的功能就容易上手多了

datagridview合併表頭

1.功能說明 將連續的多個列合併成乙個新列。2.不足之處 不能合併多層。比如下圖這樣的功能是沒有的。3.使用參考.在form的建構函式裡寫下如下 utility.exgridview.isenlarged false 在datagridview的cellpaiting事件中寫如下 utility.e...

datagridview資料繫結

sqlconnection conn new sqlconnection conn.connectionstring server local uid sa pwd sa database databasename sqlcommand cmd new sqlcommand cmd.commandt...

DataGridView用法筆記

需要指定新加行的預設值的時候,可以在datagridview.defaultvaluesneeded事件裡處理。在該事件中處理除了可以設定預設值以外,還可以指定某些特定的單元格的readonly屬性等。defaultvaluesneeded 事件處理方法 private void datagridv...