请问ASP.NET 的DataGrid可以像Windows的DataGrid一样的录入数据吗?如何实现?
如用第三方控件请说明
谢谢

解决方案 »

  1.   


    <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自己再改改
      

  2.   

    webform的datagrid控件自带的属性和模版列的功能足够实现
    不需要第3方控件了
      

  3.   

    Windows的DataGrid录入数据时,如果不采用绑定模式,则都是将数据存于控件本身,
    输入最后一行数据时,自动增加一个空白行,并不及时提交,也不会更新数据库.
    在最后用户按保存时,才将数据提交到数据库.而 Web中的DataGrid 或者 Dataview本身则无法实现阶段windows form 中的功能,
    必须借助于 javascript 才能实现.上面的朋友贴出的代码是每修改一行都直接提交到数据库,而且想编辑某一行时,还要点
    击此行的"编辑"命令,对于大量的修改不合用.windows form 和 web 的差别现在太大了.
      

  4.   

    由于 .net framework升级太快,建议软件中尽量不要使用扩充的 web控件.
    以免为将来的升级带来麻烦.
      

  5.   

    什么年代呀 DataGrid?
    现在VS2008都快淘汰了,VS2010都快出来了,还是学用新的工具好。
      

  6.   

    <!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>