我搜索了帖子还是不会操作datagrid,请大家帮助初学者

ice119103 2003-12-12 08:58:28
想实现按删除按钮删除光标所指的行,按修改按钮保存修改的数据,请给出实例代码,或者发到我的邮箱,谢谢,急需!ice119103@yahoo.com.cn
...全文
78 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ice119103 2003-12-14
Dim connStr As String
connStr = "Server=(local);DataBase=user_Info;uid=sa;pwd="
Dim cn As SqlConnection = New SqlConnection(connStr)
cn.Open()
Dim strsql As String
strsql = "select ygid as 编号,ygname as 姓名,ygdept as 部门,ygsex as 性别,ygbirthday as 生日,ygplace as 籍贯, yglevel as 学历,ygspecial as 专业,ygdate1 as 参加工作时间,ygdate2 as 进入公司时间,ygdate3 as 职称时间, ygdate4 as 进入公司时间, ygbusi as 职称,ygdate5 as 起薪时间,ygdate6 as 调入时间,ygno as 档号, ygstatus as 原身份,ygduty as 原职务,ygoffice as 原工作单位, ygremark1 as 备注 from manrecord"
Dim ds As New DataSet()
Dim da As SqlDataAdapter = New SqlDataAdapter(strsql, cn)
Dim table As DataTable
If (DataGrid1.CurrentRowIndex >= 0) Then
table = ds.Tables("manrecord")
table.DefaultView.Delete(DataGrid1.CurrentRowIndex)
If (ds.HasChanges()) Then
da.Update(ds, "manrecord")
End If
ds.Tables("manrecord").AcceptChanges()
Else
MsgBox("请选择一行数据")
End If
cn.Close()
End Sub
我这样写还是不对,不知道有什么问题
  • 打赏
  • 举报
回复
ice119103 2003-12-13
我想要做的是winform的形式
  • 打赏
  • 举报
回复
孟子E章 2003-12-12
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=9EEAFE96-3393-4228-A19C-C85A1456D104

http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=D4F5E756-0D33-402D-B40D-48E657BD3D4F

http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=81277B42-361C-4FAB-9A1E-B661ABE517F5
  • 打赏
  • 举报
回复
elite2018 2003-12-12
SqlCommand sqlCommandDel = new SqlCommand(sql_Del,connDel);
sqlCommandDel.ExecuteNonQuery();
connDel.Close();

sql = "Select * FROM People";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();

}

</script>

<font face="arial" size="3">
<b>Edit People</b>
</font>
<br>

<form runat="server">

<asp:DataGrid id="People" runat="server"
BorderColor="green"
Width="640"
PageSize="5"
AllowPaging="true"
OnPageIndexChanged="Page_Grid"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false"
ShowHeader="true"
Visible="true"

OnEditCommand="People_Edit"
OnCancelCommand="People_Cancel"
OnUpdateCommand="People_Update"
OnDeleteCommand="People_Delete">

<HeaderStyle BorderColor="White" BackColor="black"
ForeColor="White"
Font-Bold="True"
Font-Name="Arial"
Font-Size="9" HorizontalAlign="Center"/>

<ItemStyle BorderColor=""
BackColor="#FFFFF0"
ForeColor="Black"
Font-Name="Arial"
Font-Size="8"
Font-Bold="False" HorizontalAlign="Center"/>

<EditItemStyle BorderColor=""
BackColor="#FFFFF0"
ForeColor="Black"
Font-Name="Arial"
Font-Size="7"
Font-Bold="False" HorizontalAlign="Center"/>

<PagerStyle Mode="NumericPages" Font-Size="8"/>

<Columns>

<asp:BoundColumn HeaderText="ID" ReadOnly="true" DataField="pkID"/>

<asp:TemplateColumn>

<HeaderTemplate>
<b> First Name </b>
</HeaderTemplate>

<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "FirstName").ToString().Trim() %>'
runat="server"/>
</ItemTemplate>


<EditItemTemplate>
<asp:TextBox id="FirstName" Text='
<%# DataBinder.Eval(Container.DataItem, "FirstName").ToString().Trim() %>'
runat="server" Width="100%"/>
</EditItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn>

<HeaderTemplate>
<b> Last Name </b>
</HeaderTemplate>

<ItemTemplate>
<asp:Label
Width="200"
Text='<%# DataBinder.Eval(Container.DataItem, "LastName").ToString().Trim() %>'
runat="server"/>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox id="LastName" Text='<br>
<%# DataBinder.Eval(Container.DataItem, "LastName").ToString().Trim() %>'
runat="server" Width="100%"/>
</EditItemTemplate>

</asp:TemplateColumn>

<asp:EditCommandColumn
ButtonType="LinkButton"
CancelText="Cancel"
EditText="Edit"
UpdateText="Update" />

<asp:ButtonColumn Text= "Delete" CommandName="Delete"></asp:ButtonColumn>

</Columns>

</asp:DataGrid>

</form>

  • 打赏
  • 举报
回复
elite2018 2003-12-12
下面的这个例子提供了利用DataGrid编辑、修改、删除记录的方法,数据库字段名称和类型如下。

数据库aa中表 people:People.sql

if exists (select * from dbo.sysobjects where
id = object_id(N'[dbo].[People]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[People]
GO

CREATE TABLE [dbo].[People] (
[pkID] [int] IDENTITY (1, 1) NOT NULL ,
[FirstName] [char] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[LastName] [char] (100) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

EditDataGridCS.aspx

<%@ Page Language="c#" debug="true"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

//make first sql
String sql = "";
String strCnn = "Data Source=.;Initial Catalog=aa;User Id=sa;Password=;";
//create a datasource function
public ICollection CreateDataSource () {
SqlConnection conn = new SqlConnection(strCnn);

SqlDataAdapter db_sqladaptor = new SqlDataAdapter(sql,conn);

DataSet ds = new DataSet();
db_sqladaptor.Fill(ds,"MyDataResult");

DataView myView = ds.Tables["MyDataResult"].DefaultView;
return myView;
}


//do page load

public void Page_Load(Object sender, EventArgs e) {
strCnn = "Data Source=.;Initial Catalog=aa;User Id=sa;Password=;";

if (!IsPostBack)
{
sql = "Select * FROM People";
People.DataSource = CreateDataSource();
People.DataBind();
}

}

public void Page_Grid(Object sender, DataGridPageChangedEventArgs e)
{
sql = "Select * FROM People";
// Set CurrentPageIndex to the page the user clicked.
People.CurrentPageIndex = e.NewPageIndex;

// Rebind the data.
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Edit(Object sender, DataGridCommandEventArgs e)
{
sql = "Select * FROM People";

People.EditItemIndex = e.Item.ItemIndex;
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Cancel(Object sender, DataGridCommandEventArgs e)
{
sql = "Select * FROM People";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Update(Object sender, DataGridCommandEventArgs e)
{
string FirstName = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string LastName = ((TextBox)e.Item.Cells[2].Controls[1]).Text;

SqlConnection connUpdate = new SqlConnection(strCnn);
connUpdate.Open();
String sql_edit = "UPDATE People " +
"SET FirstName = '" + FirstName.Replace("'","''")+ "'," +
"LastName = '" + LastName.Replace("'","''")+ "'" +
" WHERE pkID = " + e.Item.Cells[0].Text;

SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connUpdate);
sqlCommandUpdate.ExecuteNonQuery();
connUpdate.Close();

sql = "Select * FROM People";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Delete(Object sender, DataGridCommandEventArgs e) {

SqlConnection connDel = new SqlConnection(strCnn);
connDel.Open();
String sql_Del = "DELETE FROM People " +
" WHERE pkID = " + e.Item.Cells[0].Text;

  • 打赏
  • 举报
回复
相关推荐
发帖
VB
加入

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2003-12-12 08:58
社区公告
暂无公告