如何在DataGridView控件中增加、删除和修改记录(用程序代码的方法)
即在程序运行中,手动修改DataGridView中的某个数或内容,点击某个按钮实现对数据库的更新!新手上路,望高手指点,万分感谢!!! --------------------编程问答-------------------- winform or webform?
给C#的代码行么? --------------------编程问答-------------------- 给DATAGRID加一个LINKBUTTON或者BUTTON CLOUMN
页面中:
<asp:datagrid id="DataGrid1" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 104px"
runat="server" Width="994px" Height="294px" DataKeyField="ID" AllowPaging="True" AutoGenerateColumns="False" AllowSorting="True" BackColor="SkyBlue"
ForeColor="Black" BorderColor="Gray" BorderWidth="3px" CellSpacing="1" OnEditCommand="Operation_Edit" OnDeleteCommand="Operation_Delete">
程序中:
public void Operation_Edit(object sender,DataGridCommandEventArgs e) //打开超级连接,传个值ID到弹出窗口
{
string sItemIndexOperation_No=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string urlx="/QuesMana/Details.aspx?ItemIndexOperation_No="+sItemIndexOperation_No;
string URL ="<script language='javascript'>window.open('"+urlx+"','详细信息', 'width=680,height=500,status=no,resizable=yes,top=70,left=150')</script>";
Response.Write(URL); //打开详细信息
}
public void Operation_Delete(object sender,DataGridCommandEventArgs e) //删除记录
{
string ID=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string delSQL="delete from QuesSolve where QuesID="+ID+" delete from QuesInfo where ID="+ID+"";
try
{
DB.Command(delSQL).ExecuteNonQuery();
DB.Close();
}
catch(System.Exception ex)
{
Button2.Visible=false;
Button3.Visible=false;
Button4.Visible=false;
Button5.Visible=false;
Label1.Visible=false;
Label2.Visible=false;
TextBox1.Visible=false;
DropDownList1.Visible=false;
Button1.Visible=false;
Button6.Visible=false;
DataGrid1.Visible=false;
Response.Write("数据库未能打开,请检查数据库连接.详细信息:"+ex);
}
Display();
//string JavaScript="<script language='javascript'>window.location.reload()</script>";
//Response.Write(JavaScript); //刷新当前页面
} --------------------编程问答-------------------- 用vb.net的 --------------------编程问答-------------------- Private G_SaveState As Integer = -1
Private Sub 增加_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
G_SaveState = 0
End Sub
Private Sub 修改_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_amend.Click
G_SaveState = 1
End Sub
Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
Dim da As DataRow
Try
If G_SaveState = 0 Then
'增加
da = table.NewRow()
da(0) = txtbox1.text
da(1) = txtbox2.text
table.Rows.Add(da)
MsgBox("保存完毕", MsgBoxStyle.OkOnly, "确认")
'修改
ElseIf G_SaveState = 1 Then
dr(0)(0) = txtbox1.text
dr(0)(1) = txtbox2.text
MsgBox("修改完毕", MsgBoxStyle.OkOnly, "确认")
End If
bc.UpdateTable(table)
Me.datagrid.Rows.Clear()
Catch ex As Exception
End Try
End Sub
Public Function UpdateTable(ByVal tb As DataTable) As Boolean
UpdateTable = True
Try
If con.State = ConnectionState.Closed Then
con.Open()
End If
dap.Update(tb)
Catch ex As Exception
UpdateTable = False
Throw ex
Finally
con.Close()
End Try
End Function
Public Function SelectTable(ByVal tablename As String, ByVal tb As DataTable) As Boolean
SelectTable = True
Try
'DataBase连接字符窜生成
con = New SqlConnection(conStr)
'DataBase连接
If con.State = ConnectionState.Closed Then
con.Open()
End If
'table 连接字符窜
Dim sqlStr = "select * from " & tablename
dap = New SqlDataAdapter(sqlStr, con)
bld = New SqlCommandBuilder(dap)
tb.Clear()
tb.AcceptChanges()
dap.Fill(tb)
Catch ex As Exception
SelectTable = False
Throw ex
Finally
con.Close()
End Try
End Function
End Class --------------------编程问答-------------------- 错误 1 未声明名称“table”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 33 22 user11
错误 2 未声明名称“txtbox1”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 34 25 user11
错误 3 未声明名称“txtbox2”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 35 25 user11
错误 4 未声明名称“table”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 36 17 user11
错误 5 未声明名称“dr”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 42 17 user11
错误 6 未声明名称“txtbox1”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 42 28 user11
错误 7 未声明名称“dr”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 43 17 user11
错误 8 未声明名称“txtbox2”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 43 28 user11
错误 9 未声明名称“bc”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 49 13 user11
错误 10 未声明名称“table”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 49 28 user11
错误 11 “datagrid”不是“user11.Form2”的成员。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 50 13 user11
错误 12 未声明名称“con”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 57 16 user11
错误 13 未声明名称“con”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 58 17 user11
错误 14 未声明名称“dap”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 60 13 user11
错误 15 未声明名称“con”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 65 13 user11
错误 16 未声明名称“con”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 72 13 user11
错误 17 未定义类型“SqlConnection”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 72 23 user11
错误 18 未声明名称“con”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 74 16 user11
错误 19 未声明名称“con”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 75 17 user11
错误 20 未声明名称“dap”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 79 13 user11
错误 21 未定义类型“SqlDataAdapter”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 79 23 user11
错误 22 未声明名称“bld”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 80 13 user11
错误 23 未定义类型“SqlCommandBuilder”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 80 23 user11
错误 24 未声明名称“dap”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 83 13 user11
错误 25 未声明名称“con”。 C:\Documents and Settings\Administrator\桌面\user11\user11\Form2.vb 88 13 user11
--------------------编程问答-------------------- 求分... --------------------编程问答-------------------- 改dataset能够完成你的任务,就是不知道你愿意不愿意
'----------------------查找dataset
For i As Integer = 0 To Zbq666AccessDataSet1.发送器建立的方案.Rows.Count - 1
If Zbq666AccessDataSet1.发送器建立的方案.Rows(i).Item(1) = Me.TextBox5.Text.Trim.ToString Then
MsgBox("此方案已经有了,换个名称试试")
Exit Sub
End If
Next
'-------------------添加到dataset
Try
Dim dr As DataRow = Zbq666AccessDataSet1.发送器建立的方案.New发送器建立的方案Row
dr("id") = Me.Zbq666AccessDataSet1.发送器建立的方案.Rows.Count + 1
dr(1) = Me.TextBox5.Text.Trim
dr("方案制定人") = Me.TextBox2.Text.Trim
dr("密码") = Me.TextBox3.Text.Trim
dr("建立时间") = DateAndTime.Now
Zbq666AccessDataSet1.发送器建立的方案.Add发送器建立的方案Row(dr)
Catch ex As Exception
MsgBox(ex.Message)
End Try
'刷新显示------------------------------
Me.发送器建立的方案DataGridView.DataSource = 发送器建立的方案BindingSource
‘保存
Try
Me.Validate()
Me.发送器测试方案BindingSource.EndEdit()
Me.发送器建立的方案BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Zbq666AccessDataSet1)
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
MsgBox("保存成功", vbOKOnly)
Me.发送器测试方案DataGridView.ReadOnly = True
'删除数据集中的数据-------------------
Dim i As Integer = Me.发送器建立的方案DataGridView.CurrentRow.Index
If Zbq666AccessDataSet1.发送器建立的方案.Rows(i)("密码") = Me.TextBox6.Text.Trim Then
Zbq666AccessDataSet1.发送器建立的方案.Rows(i).Delete()
Try
Me.Validate()
Me.发送器测试方案BindingSource.EndEdit()
Me.发送器建立的方案BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Zbq666AccessDataSet1)
Catch ex As Exception
MsgBox(ex.Message)
End Try
补充:.NET技术 , VB.NET