DataGrid 请问ASP.NET 的DataGrid可以像Windows的DataGrid一样的录入数据吗?如何实现?如用第三方控件请说明谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="Wno" HeaderText="错误编号" ReadOnly="True" /> <asp:BoundField DataField="Cname" HeaderText="编码者" /> <asp:BoundField DataField="Fname" HeaderText="发现者" /> <asp:BoundField DataField="Matter" HeaderText="错误内容" /> <asp:CheckBoxField DataField="YN" HeaderText="是否修改" > <ControlStyle BackColor="Red" ForeColor="Lime" /> </asp:CheckBoxField> <asp:CommandField HeaderText="选择" ShowSelectButton="True" /> <asp:CommandField HeaderText="编辑" ShowEditButton="True" /> <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> </Columns> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> <EditRowStyle BackColor="#2461BF" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView>protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } private void bind() { string sql = "select Wno,Cname,Fname,Matter,YN from Cuser"; DataSet ds = new DataSet(); ds = Conn.getDataSet(sql); GridView1.DataSource = ds.Tables[0]; GridView1.DataKeyNames = new string[] { "Wno" }; GridView1.DataBind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from Cuser where Wno='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; Conn.executeDML(sqlstr); bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string sqlstr = "update Cuser set Cname='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Fname='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Matter='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',YN='" +((CheckBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Checked.ToString()+"'where Wno='" + GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim() + "'"; Conn.executeDML(sqlstr); GridView1.EditIndex = -1; bind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) { ((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[0].Text + "\"吗?')"); } } }LZ自己再改改 webform的datagrid控件自带的属性和模版列的功能足够实现不需要第3方控件了 Windows的DataGrid录入数据时,如果不采用绑定模式,则都是将数据存于控件本身,输入最后一行数据时,自动增加一个空白行,并不及时提交,也不会更新数据库.在最后用户按保存时,才将数据提交到数据库.而 Web中的DataGrid 或者 Dataview本身则无法实现阶段windows form 中的功能,必须借助于 javascript 才能实现.上面的朋友贴出的代码是每修改一行都直接提交到数据库,而且想编辑某一行时,还要点击此行的"编辑"命令,对于大量的修改不合用.windows form 和 web 的差别现在太大了. 由于 .net framework升级太快,建议软件中尽量不要使用扩充的 web控件.以免为将来的升级带来麻烦. 什么年代呀 DataGrid?现在VS2008都快淘汰了,VS2010都快出来了,还是学用新的工具好。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><style type="text/css"><!-- td{width:300px;text-align:center;margin:0px;padding:0px}--></style><script language="javascript" type="text/javascript"><!-- function show(){ var num=document.getElementById("test").getElementsByTagName("input")[0].value;var y=parseInt(num);for(var i=0;i<y;i++){var strS='<table cellpadding="0" cellspacing="0" border="1px solid black"><tr><td><select style="width:102px"><option>选择产品编号</option></select></td><td><input type="text" style="width:99px" /></td><td><input type="text" style="width:100px" />(数字)</td><td><input type="text" style="width:100px" /></td></tr></table>';document.getElementById("change").innerHTML+=strS;} }//--></script><div id="test">添加产品品种数:<input type="text" name="text" value="" /><input type="button" name="submit" value="生成输入框" onclick="show()" /> </div><table cellpadding="0" cellspacing="0" border="1px solid #ccc"><tr style="background-color:#009999"><td>产品编号</td><td>产品名称</td><td>数量</td><td>单价</td></tr></table><div id="change"></div></body></html> Jquery 在程序中是怎样的一个执行顺序? 如何设置GRIDVIEW1中每个ROWS的宽度? 全局变量在asp.net的html里面该如何写? 在一个文本框输入后直接按回车键后光标可自动跳到下一文本框 用DW2004想做一个Blog的回复页,可是按了提交后留言内容丢失 请问怎样来调用存储过程? 在数据库有一字段ID,如何得到其中没有被占用的最小ID值,如何快速得到? asp.net + c# + excel???? 一个很简单的问题! 如何实现web页面自动刷新 asp.net获取客户端的MAC地址 调用存储过程分页的简单问题
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Wno" HeaderText="错误编号" ReadOnly="True" />
<asp:BoundField DataField="Cname" HeaderText="编码者" />
<asp:BoundField DataField="Fname" HeaderText="发现者" />
<asp:BoundField DataField="Matter" HeaderText="错误内容" />
<asp:CheckBoxField DataField="YN" HeaderText="是否修改" >
<ControlStyle BackColor="Red" ForeColor="Lime" />
</asp:CheckBoxField>
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
string sql = "select Wno,Cname,Fname,Matter,YN from Cuser";
DataSet ds = new DataSet();
ds = Conn.getDataSet(sql);
GridView1.DataSource = ds.Tables[0];
GridView1.DataKeyNames = new string[] { "Wno" };
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from Cuser where Wno='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
Conn.executeDML(sqlstr);
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlstr = "update Cuser set Cname='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Fname='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Matter='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',YN='"
+((CheckBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Checked.ToString()+"'where Wno='"
+ GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim() + "'";
Conn.executeDML(sqlstr);
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[0].Text + "\"吗?')");
}
}
}LZ自己再改改
不需要第3方控件了
输入最后一行数据时,自动增加一个空白行,并不及时提交,也不会更新数据库.
在最后用户按保存时,才将数据提交到数据库.而 Web中的DataGrid 或者 Dataview本身则无法实现阶段windows form 中的功能,
必须借助于 javascript 才能实现.上面的朋友贴出的代码是每修改一行都直接提交到数据库,而且想编辑某一行时,还要点
击此行的"编辑"命令,对于大量的修改不合用.windows form 和 web 的差别现在太大了.
以免为将来的升级带来麻烦.
现在VS2008都快淘汰了,VS2010都快出来了,还是学用新的工具好。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<style type="text/css">
<!--
td{width:300px;text-align:center;margin:0px;padding:0px}
-->
</style>
<script language="javascript" type="text/javascript">
<!--
function show(){
var num=document.getElementById("test").getElementsByTagName("input")[0].value;
var y=parseInt(num);
for(var i=0;i<y;i++){
var strS='<table cellpadding="0" cellspacing="0" border="1px solid black"><tr><td><select style="width:102px"><option>选择产品编号</option></select></td><td><input type="text" style="width:99px" /></td><td><input type="text" style="width:100px" />(数字)</td><td><input type="text" style="width:100px" /></td></tr></table>';
document.getElementById("change").innerHTML+=strS;
}
}
//-->
</script>
<div id="test">
添加产品品种数:<input type="text" name="text" value="" />
<input type="button" name="submit" value="生成输入框" onclick="show()" />
</div>
<table cellpadding="0" cellspacing="0" border="1px solid #ccc">
<tr style="background-color:#009999">
<td>产品编号</td>
<td>产品名称</td>
<td>数量</td>
<td>单价</td>
</tr>
</table>
<div id="change">
</div>
</body>
</html>