1.程序功能:当点击删除行按钮时,删除当前选定的行,当点击删除列按钮时,删除输入的列。
2.程序设计:主要利用删除数据集中的相关行或列来实现程序功能
3.窗体设计
打开MiscrosoftVisualStudio.NET,选择新建项目,在项目类型中选择VisualBasic项目,
在模板中选择Windows应用程序,在名称中输入DGStyle,选择保存位置。青年人网提示再点击确定,打开设计窗口,
向窗体中添加一个DataGrid控件和两个Label控件。控件属性Label1.Text="删除行";
Label2.Text="删除列";DataGrid1.CaptionText="DataGrid删除行.列"
4.代码设计:
ImportsSystem.Data.SqlClient
PublicClassForm1
InheritsSystem.Windows.Forms.Form
dimtbasDataTable
'窗体代码略
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
DimsconAsNewSqlConnection("server=localhost;database=northwind;integratedsecurity=true")
'定义一个数据适配器sda获取雇员表中的雇员IDlastname
DimsdaAsNewSqlDataAdapter("selectemployeeid,lastnamefromemployeesorderbyemployeeid",scon)
DimdsAsNewDataSet
Try
sda.Fill(ds,"employees")
CatchexAsException
EndTry
tb=ds.Tables(0)
DataGrid1.DataSource=tb
EndSub
'删除行按钮
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimnAsInteger
'获取当前选定的行号
n=DataGrid1.CurrentRowIndex
'从数据集集合中删除行
tb.Rows.RemoveAt(n)
'刷新Datagrid1显示删除后的数据
DataGrid1.Refresh()
EndSub
'删除列按钮
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
DimcolumnNameAsString
'调用InPutbox函数输入字段名
columnName=InputBox("请输入要删除的字段名","删除列")
'判断字段是否存在
Iftb.Columns.Contains(columnName)=TrueThen
tb.Columns.Remove(columnName)
DataGrid1.DataSource=tb
Else
MsgBox("字段名不对,请重新输入")
ExitSub
EndIf
EndSub
EndClass
责任编辑:小草