DataGrid编辑,删除问题

cooleader123 2004-05-06 03:50:31
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid2" runat="server" GridLines="Horizontal" CellPadding="3" BackColor="White"
BorderWidth="1px" BorderStyle="None" BorderColor="#E7E7FF" AutoGenerateColumns="False" Height="24px"
Width="503px" OnDeleteCommand="DEDR_Delete" OnCancelCommand="DEDR_Cancel" OnUpdateCommand="DEDR_Update"
OnEditCommand="DEDR_Edit">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="报名ID">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"报名ID")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="报名ID" Text='<%# DataBinder.Eval(Container.DataItem,"报名ID")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="姓名">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"姓名")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="姓名" Text='<%# DataBinder.Eval(Container.DataItem,"姓名")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="语文">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"语文")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="语文" Text='<%# DataBinder.Eval(Container.DataItem,"语文")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="数学">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"数学")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="数学" Text='<%# DataBinder.Eval(Container.DataItem,"数学")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="英语">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"英语")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="英语" Text='<%# DataBinder.Eval(Container.DataItem,"英语")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button CommandName="Edit" Text="编辑" Runat="server" ID="Button1"></asp:Button>
<asp:Button CommandName="Delete" Text="删除" Runat="server" ID="Button2"></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button CommandName="Cancel" Text="取消" Runat="server" ID="Button3"></asp:Button>
<asp:Button CommandName="Update" Text="更新" Runat="server" ID="Button4"></asp:Button>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:button id="add" runat="server" Height="27px" Width="60px" Text="添加新项"
style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 200px"></asp:button>
</form>

以上的是data2.apsx文件

以下的是data2.apsx.cs文件

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

private void Page_Load(object sender, System.EventArgs e)
{
if(!(Page.IsPostBack))
{
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
}// 在此处放置用户代码以初始化页面
}

private void add_Click(object sender, System.EventArgs e)
{
DataSet dataSet=LoadData();
DataRow newrow;
newrow=dataSet.Tables["Grade"].NewRow();
newrow["报名ID"]="";
newrow["姓名"]="";
newrow["语文"]="";
newrow["数学"]="";
newrow["英语"]="";
dataSet.Tables["Grade"].Rows.Add(newrow);
SqlDataAdapter objAdapter=new SqlDataAdapter();
SqlCommandBuilder objBuilder=new SqlCommandBuilder(objAdapter);
objAdapter.UpdateCommand=objBuilder.GetUpdateCommand();
objAdapter.DeleteCommand=objBuilder.GetDeleteCommand();
objAdapter.InsertCommand=objBuilder.GetInsertCommand();
objAdapter.Update(dataSet,"Grade");
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
DataGrid2.EditItemIndex=DataGrid2.Items.Count-1;
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
}

public DataSet LoadData()
{
string strConnection = "server=localhost;user id=sa;password=sa;";
strConnection += "initial catalog=universitysystem;";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
string strSQL="select * from EntryExamGrade";
SqlDataAdapter objAdapter=new SqlDataAdapter(strSQL,objConnection);
DataSet objDataSet=new DataSet();
objAdapter.Fill(objDataSet,"Grade");
return objDataSet;
}

public void DEDR_Edit(object sender,DataGridCommandEventArgs e)
{
DataGrid2.EditItemIndex=Convert.ToInt32(e.Item.ItemIndex);
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
}

public void DEDR_Update(object sender,DataGridCommandEventArgs e)
{
DataSet dataSet=LoadData();
int row=Convert.ToInt32(e.Item.ItemIndex);
TextBox EditText=null;
EditText=(TextBox)e.Item.FindControl("语文");
dataSet.Tables["Grade"].Rows[row]["语文"]=EditText.Text;
EditText=(TextBox)e.Item.FindControl("数学");
dataSet.Tables["Grade"].Rows[row]["数学"]=EditText.Text;
EditText=(TextBox)e.Item.FindControl("英语");
dataSet.Tables["Grade"].Rows[row]["英语"]=EditText.Text;
SqlDataAdapter objAdapter=new SqlDataAdapter();
SqlCommandBuilder objBuilder=new SqlCommandBuilder(objAdapter);
objAdapter.UpdateCommand=objBuilder.GetUpdateCommand();
objAdapter.DeleteCommand=objBuilder.GetDeleteCommand();
objAdapter.InsertCommand=objBuilder.GetInsertCommand();
objAdapter.Update(dataSet,"Grade");
DataGrid2.EditItemIndex=-1;
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
}

public void DEDR_Cancel(object sender,DataGridCommandEventArgs e)
{
DataGrid2.EditItemIndex=-1;
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
}

public void DEDR_Delete(object sender,DataGridCommandEventArgs e)
{
DataSet dataSet=LoadData();
int row=Convert.ToInt32(e.Item.ItemIndex);
dataSet.Tables["Grade"].Rows[row].Delete();
SqlDataAdapter objAdapter=new SqlDataAdapter();
SqlCommandBuilder objBuilder=new SqlCommandBuilder(objAdapter);
objAdapter.UpdateCommand=objBuilder.GetUpdateCommand();
objAdapter.DeleteCommand=objBuilder.GetDeleteCommand();
objAdapter.InsertCommand=objBuilder.GetInsertCommand();
objAdapter.Update(dataSet,"Grade");
DataGrid2.EditItemIndex=-1;
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
}

}


程序是用VS.NET(C#)编的,但是运行时屏幕上什么都不显示,只单独显示了一个按钮,也没有提示错误,请各位高手帮忙看看,小弟谢谢了,100分送上,由于CSDN规定了长度,所以只贴了关键的地方出来
...全文
49 9 点赞 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cooleader123 2004-05-06
TO:1979xt(VS.NET为未来十年作好了准备,你呢)
怎么可能呀?你看我的代码呀,肯定是绑定了的
  • 打赏
  • 举报
回复
1979xt 2004-05-06
没显示说明数据没绑定.
  • 打赏
  • 举报
回复
linfuguo 2004-05-06
<ASP:DataGrid id="MemberData" runat="server" Font-Names="宋体" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="#aaaadd" Font-Size="X-Small" Font-Name="Verdana" CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1" BorderColor="black" OnDeleteCommand="MemberData_Delete" OnCancelCommand="MemberData_Cancel" OnUpdateCommand="MemberData_Update" OnEditCommand="MemberData_Edit" AutoGenerateColumns="false" OnPageIndexChanged="MemberData_Page" DataKeyField="ID" AllowPaging="True">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="用户帐户">
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("useid")%>' runat="server" ID="Label1"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="useid_up" Text='<%# Container.DataItem("useid")%>' Size="10" runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="密码">
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("pass")%>' runat="server" ID="Label2"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="pass_up" Text='<%# Container.DataItem("pass")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="用户权限表">
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("qxian")%>' ItemStyle-Wrap="false" runat="server" ID="Label4"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="qxian_up" Text='<%# Container.DataItem("qxian")%>' runat="server" ReadOnly=True/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="用户名称">
<ItemTemplate>
<asp:Label Text='<%# Container.DataItem("username")%>' ItemStyle-Wrap="false" runat="server" ID="Label3"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="username_up" Text='<%# Container.DataItem("username")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
</Columns>
<PagerStyle NextPageText="后一页" PrevPageText="前一页" HorizontalAlign="Center"></PagerStyle>
</ASP:DataGrid>
  • 打赏
  • 举报
回复
linfuguo 2004-05-06
'定义函数:删除用户数据
Sub MemberData_Delete(Sender As Object, E As DataGridCommandEventArgs)
Dim delCommand As SqlCommand
Dim delMember as String
Dim conn AS SqlConnection

'用于记录的变量,用来查找到对应的表,删除对应的表
Dim deltable as String
'用于进行删除表的操作
Dim myParam AS SqlParameter
Dim paramName As String
Dim paramNumber As Integer
'提示begin
'end

conn = New SqlConnection(SqlServer)
conn.open()

delMember = "Delete from tb_quser Where id = @ID"
delCommand = New SqlCommand(delMember, conn)
delCommand.Parameters.Add(new SqlParameter("@ID", SqldbType.nVarChar, 50))
delCommand.Parameters.Item("@ID").Value = MemberData.DataKeys.Item(E.Item.ItemIndex)
deltable=delCommand.Parameters.Item("@ID").Value
'response.write(deltable)
'#################################################
'paramNumber=1
'response.write("这 是:"&delCommand.Parameters.Count)
'在此把一个TextBox控件进行改变id

'myParam = delCommand.Parameters(paramNumber)

'paramName = myParam.ParameterName
'memberFormData = E.Item.FindControl(paramName.SubString(1)&"_up")
'delCommand.Parameters.Item(paramName).Value = memberFormData.Text
'Response.Write("<br>")
'response.write("这是值:"&delCommand.Parameters.Item(paramName).Value)
'response.end()

'######################################################
'读取
Dim conn1 as SqlConnection
Dim conn2 as Sqlconnection
Dim deltablestr as string
Dim dbComm as SqlDataAdapter
Dim ds as New DataSet
Dim deltablestr1 as string
Dim delorder as string

try
conn1 = New SqlConnection(SqlServer)
conn1.open()
deltablestr="select * from tb_quser Where id="&deltable
'response.write(deltablestr)
'response.end()
dbComm = New SqlDataAdapter(deltablestr,conn1)
dbComm.Fill(ds,"tb_quser")
DropDownList1.DataSource = ds.Tables("tb_quser").Defaultview
DropDownList1.DataTextField="qxian"
DropDownList1.DataValueField="qxian"
DropDownList1.DataBind()
'在提出变量的值时必须加入到此处
'response.write(DropDownList1.SelectedItem.Text)
'response.write("<br>")
delorder=DropDownList1.SelectedItem.Text
'response.write(delorder)

conn1.close()
catch ee as exception
info.text=" append error:"+ee.message
finally
end try

'把删除表的语句在此实现
delCommand.ExecuteReader()
MemberData.EditItemIndex = -1
BindMemberData()
conn.close()
'del table

'定义一个用于进行删除表的字符串!开始删除表
deltablestr1 = "DROP TABLE "&delorder
'response.write(deltablestr1)
'response.end()
try

conn2 = New SqlConnection(SqlServer)
dim mycommand2 as sqlcommand=new sqlcommand(deltablestr1,conn2)
conn2.open()
dim mydatareader2 as sqldatareader = mycommand2.executereader()
conn2.close()
catch ee as exception
info.text=" append error:"+ee.message
finally
end try
'Response.write(deltablestr1)
'DROP TABLE DropDownList1.SelectedItem.Text
info.text="删除成功!"
'response.end()

End Sub

'进行分页显示
Sub MemberData_Page(sender as object,e as DataGridPageChangedEventArgs)
'Sender As Object, E As DataGridCommandEventArgs)

   'MemberData.CurrentPageIndex =e.NewPageIndex;
   'BindMemberData();
   '不能加分号!
MemberData.CurrentPageIndex =e.NewPageIndex
BindMemberData()

End Sub
'定义函数:显示用户数据
Sub BindMemberData()

Dim conn AS SqlConnection
Dim dbComm AS SqlDataAdapter
Dim Sql AS String
'response.write("<br>")

Dim ds AS New DataSet
'在建立此对像的时候,请注意它的名子空间
conn = New SqlConnection(Sqlserver)

Sql = "Select * from tb_quser ORDER BY ID"
'response.write(Sql)

dbComm = New SqlDataAdapter(Sql,conn)
dbComm.Fill(ds,"tb_quser")
MemberData.DataSource = ds.Tables("tb_quser").DefaultView
MemberData.DataBind()
'response.end()
End Sub
  • 打赏
  • 举报
回复
linfuguo 2004-05-06
下面是vb.net,而且还没有分页,希望能给你帮助!
Private Sqlserver AS String
'定义当页面重新装载时的事件
Sub Page_Load(myList AS Object,E as EventArgs)
Sqlserver = "server=192.168.0.5;uid=sa;pwd=dartfar;database=gd_sj"
'Sqlserver = Application["MySource"]
' response.write(Sqlserver)
'response.end()
'MemberFunctionUpdate是两个容器里的一个
If NOT Page.IsPostBack
MemberFunctionUpdate.Visible = true
MemberFunctionAdd.Visible = false
'info.text=""
BindMemberData()
End If
info.text=""
End Sub

'定义函数Panel的可见控件
Sub PanelReset()
MemberFunctionUpdate.Visible = true
MemberFunctionAdd.Visible = false
End Sub

'定义函数:新增用户时的可见控件
Sub newUser(sender As Object , e As EventArgs)
MemberFunctionUpdate.Visible = false
MemberFunctionAdd.Visible = true
End Sub

'定义函数:取消成员数据编辑时候的事件
Sub MemberData_Cancel(Sender As Object, E As DataGridCommandEventArgs)
MemberData.EditItemIndex = -1
BindMemberData()
End Sub

'定义函数:编辑成员数据编辑时候的事件
Sub MemberData_Edit(Sender As Object, E As DataGridCommandEventArgs)
MemberData.EditItemIndex = E.Item.ItemIndex
BindMemberData()
End Sub

'定义函数:取消新增用户时进入的页面
Sub OnCancel(sender As Object , e As System.EventArgs)
Response.Redirect("modifyuser.aspx")
End Sub

'定义函数:更新用户数据
Sub MemberData_Update(Sender As Object, E As DataGridCommandEventArgs)
Dim updateMember As String
Dim upCommand As SqlCommand
Dim myParam AS SqlParameter
Dim memberFormData As TextBox
Dim connState As Integer
Dim paramNumber As Integer
Dim paramName As String
Dim conn AS SqlConnection
'ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconusingstoredprocedureswithcommand.htm
try
conn = New SqlConnection(SqlServer)
conn.Open()

updateMember = "Update tb_quser Set useid = @useid,"
updateMember = updateMember & "pass=@pass,qxian=@qxian,username=@username"
updateMember = updateMember & " Where id = @ID"
'response.write(updateMember)
'response.end()
upCommand = New SqlCommand(updateMember, conn)
upCommand.Parameters.Add(new SqlParameter("@ID", SqldbType.nVarChar, 50))
upCommand.Parameters.Add(new SqlParameter("@useid", SqldbType.nVarChar, 50))
upCommand.Parameters.Add(new SqlParameter("@pass", SqldbType.nVarChar, 50))
'加入这一个,完全是用于在删除程序中对其表进行操作
upCommand.Parameters.Add(new SqlParameter("@qxian", SqldbType.nVarChar, 50))
upCommand.Parameters.Add(new SqlParameter("@username", SqldbType.nVarChar, 50))
'ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemdatasqlclientsqlparametercollectionmemberstopic.htm
'response.write(upCommand.Parameters.count)
'response.write("<br>")
'response.write(upCommand.Parameters.Item("@ID").Value)
'response.write(updateMember)
'response.end()
upCommand.Parameters.Item("@ID").Value = MemberData.DataKeys.Item(E.Item.ItemIndex)

'如何获得datagrid的活动单元格的值
'datagrid1.items(datagrid1.selectedindex).cell(i).text
'i代表第几列

For paramNumber = 1 To upCommand.Parameters.Count - 1
'用于DateGird中的控件文本框的名字进行单个取出
myParam = upCommand.Parameters(paramNumber)
paramName = myParam.ParameterName
'response.write("<br>")
'response.write(paramName.Length)
'response.write("<br>")
'response.write(paramName)
memberFormData = E.Item.FindControl(paramName.SubString(1)&"_up")
'memberFormData是一个对像,用于进行对DateGird的属性进行控制
'response.write(memberFormData.Text)
'response.end()
upCommand.Parameters.Item(paramName).Value = memberFormData.Text
'response.write("这是第一个控件的值:"&upCommand.Parameters.Item(paramName).Value)
'response.write("<br>")
'response.write("这是id号:"&memberFormData.id)
'response.end()
Next

upCommand.ExecuteReader()
MemberData.EditItemIndex = -1
BindMemberData()

conn.close()
response.Redirect("right_m.aspx")
catch ee as exception
info.text=" append error:"+ee.message
finally
end try
info.text="修改成功!"

End Sub

  • 打赏
  • 举报
回复
QingYuan2008 2004-05-06
你加个断点试试啊!
  • 打赏
  • 举报
回复
cooleader123 2004-05-06
TO:kanshangren(小彻小悟)
你说的方法我已经试过了,没用的,还是不能显示数据
  • 打赏
  • 举报
回复
kanshangren 2004-05-06
DataGrid2.DataSource=LoadData();
DataGrid2.DataBind();
DataGrid2绑定到了一个返回的DataSet上,而不是DATATABLE,应该绑定的是
LoadData().Talbes["Grade"];
  • 打赏
  • 举报
回复
powbcom 2004-05-06
代码太多了,看起来时间挺浪费的!
你精简一点,还有空给你解答一下!
有些代码是多余的…
  • 打赏
  • 举报
回复
相关推荐
发帖
.NET社区
加入

5.9w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-05-06 03:50
社区公告
暂无公告