找回密码
 立即注册

QQ登录

只需一步,快速开始

zhuyinyin_66
高级会员   /  发表于:2018-5-22 11:08  /   查看:3822  /  回复:7
本帖最后由 zhuyinyin_66 于 2018-5-22 11:09 编辑

很多时候,我们需要可编辑的表格,来比较方便的进行数据的录入,比如学习成绩的录入。当然这就要求能够动态的增加行,来一次性录入多个学生的信息。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

7 个回复

倒序浏览
JeffryLI
葡萄城公司职员   /  发表于:2018-5-22 16:46:21
推荐
本帖最后由 JeffryLI 于 2018-5-22 18:21 编辑
zhuyinyin_66 发表于 2018-5-22 16:34
不客气,还得经常麻烦你呢

多支持就好,哈哈

评分

参与人数 1满意度 +5 收起 理由
zhuyinyin_66 + 5 很给力!

查看全部评分

请点击评分,对我5分评价,谢谢!

葡萄城控件服务团队
官方网站: https://www.grapecity.com.cn/developer
回复 使用道具 举报
JeffryLI
葡萄城公司职员   /  发表于:2018-5-22 11:43:56
沙发
您好,这个没有这个属性,不支持动态添加行的。只能自己布局一个添加的输入。希望能帮到您。

评分

参与人数 1满意度 +5 收起 理由
zhuyinyin_66 + 5 神马都是浮云

查看全部评分

请点击评分,对我5分评价,谢谢!

葡萄城控件服务团队
官方网站: https://www.grapecity.com.cn/developer
回复 使用道具 举报
zhuyinyin_66
高级会员   /  发表于:2018-5-22 13:01:34
板凳
自己已实现该功能。
回复 使用道具 举报
JeffryLI
葡萄城公司职员   /  发表于:2018-5-22 14:23:36
地板
zhuyinyin_66 发表于 2018-5-22 13:01
自己已实现该功能。

可以把这个分享出来,我可以给您送金币,大家一起学习嘛。

评分

参与人数 1满意度 +5 收起 理由
zhuyinyin_66 + 5

查看全部评分

请点击评分,对我5分评价,谢谢!

葡萄城控件服务团队
官方网站: https://www.grapecity.com.cn/developer
回复 使用道具 举报
zhuyinyin_66
高级会员   /  发表于:2018-5-22 14:45:37
5#
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title></title>
  6.     <style type="text/css">
  7.         .hide{
  8.             display:none;   
  9.          }
  10.     </style>
  11.     <script type="text/javascript">
  12.         //选中所有行
  13.         function SelectAll(chkAll)
  14.         {
  15.             var gridview = document.getElementById("GridView1");
  16.             if (gridview)
  17.             {
  18.                //获取到GridView1中的所有input标签
  19.                var inputs = gridview.getElementsByTagName("input");
  20.                for(var i=0;i<inputs.length;i++)
  21.                {
  22.                   if (inputs[i].type=="checkbox")
  23.                   {
  24.                      //设置所有checkbox的选中状态与chkAll一致
  25.                      inputs[i].checked = chkAll.checked;
  26.                   }
  27.                }
  28.             }
  29.        }
  30.       
  31.        //给选中行换背景色
  32.        function checkRow(chkRow)
  33.        {
  34.           var row = chkRow.parentNode.parentNode;
  35.           if(row)
  36.           {
  37.                if (chkRow.checked)
  38.                    row.style.backgroundColor="#7799CC";
  39.                 else
  40.                    row.style.backgroundColor="#FFFFFF";
  41.            }
  42.        }
  43.     </script>
  44. </head>
  45. <body>
  46.     <form id="form1" runat="server">
  47.    <div>
  48.        <asp:LinkButton ID="lbtnAddRow" runat="server" Width="80px" OnClick="lbtnAddRow_Click">添加行</asp:LinkButton>
  49.       <asp:LinkButton ID="btnDeleteRow" runat="server" OnClick="btnDeleteRow_Click" OnClientClick="return confirm('确定要删除选中行吗?');">删除选中行</asp:LinkButton>
  50.    </div>
  51.     <div>
  52.         <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333">
  53.             <Columns>
  54.                 <asp:BoundField DataField="ID" HeaderText="ID" >
  55.                     <ItemStyle CssClass="hide" BorderColor="#507CD1" />
  56.                     <HeaderStyle CssClass="hide" />
  57.                 </asp:BoundField>
  58.                 <asp:TemplateField HeaderText="序号">
  59.                     <ItemTemplate>
  60.                         <%# Container.DataItemIndex + 1%>
  61.                     </ItemTemplate>
  62.                     <ItemStyle BorderColor="#507CD1" HorizontalAlign="Center" BorderWidth="1px" />
  63.                 </asp:TemplateField>
  64.                 <asp:TemplateField>
  65.                     <HeaderTemplate>
  66.                        <input id="chkAll" type="checkbox" onclick="SelectAll(this)" />
  67.                     </HeaderTemplate>
  68.                     <ItemTemplate>
  69.                         <input id="chkRow" type="checkbox" onclick="checkRow(this);" runat="server" />
  70.                     </ItemTemplate>
  71.                     <ItemStyle Width="30px" HorizontalAlign="Center" BorderColor="#507CD1" BorderWidth="1px" />
  72.                 </asp:TemplateField>
  73.                 <asp:TemplateField HeaderText="姓名">
  74.                     <ItemTemplate>
  75.                         <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' BorderStyle="None"></asp:TextBox>
  76.                     </ItemTemplate>
  77.                     <ItemStyle Width="100px" BorderColor="#507CD1" BorderWidth="1px" />
  78.                 </asp:TemplateField>
  79.                 <asp:TemplateField HeaderText="平时成绩">
  80.                     <ItemTemplate>
  81.                         <asp:TextBox ID="txtUsuallyResults" runat="server" Text='<%# Bind("UsuallyResults") %>' BorderStyle="None"></asp:TextBox>
  82.                     </ItemTemplate>
  83.                     <ItemStyle Width="100px" BorderColor="#507CD1" BorderWidth="1px" />
  84.                 </asp:TemplateField>
  85.                 <asp:TemplateField HeaderText="考试成绩">
  86.                     <ItemTemplate>
  87.                         <asp:TextBox ID="txtExamResults" runat="server" Text='<%# Bind("ExamResults") %>' BorderStyle="None"></asp:TextBox>
  88.                     </ItemTemplate>
  89.                     <ItemStyle Width="100px" BorderColor="#507CD1" BorderWidth="1px" />
  90.                 </asp:TemplateField>
  91.             </Columns>
  92.             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  93.         </asp:GridView>
  94.     </div>
  95.     </form>
  96. </body>
  97. </html>
复制代码

  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         if (!IsPostBack)
  4.         {
  5.             DataTable table = new DataTable();
  6.             table.Columns.Add(new DataColumn("ID"));
  7.             table.Columns.Add(new DataColumn("Name"));
  8.             table.Columns.Add(new DataColumn("UsuallyResults"));
  9.             table.Columns.Add(new DataColumn("ExamResults"));
  10.             DataRow row = table.NewRow();
  11.             table.Rows.Add(row);
  12.             GridView1.DataSource = table;
  13.             GridView1.DataBind();
  14.         }
  15.     }

  16.     protected void lbtnAddRow_Click(object sender, EventArgs e)
  17.     {
  18.         DataTable table = GetGridViewData();
  19.         DataRow newRow = table.NewRow();
  20.         newRow["ID"] = Guid.NewGuid().ToString();
  21.         table.Rows.Add(newRow);
  22.         GridView1.DataSource = table;
  23.         GridView1.DataBind();
  24.     }

  25.     private DataTable GetGridViewData()
  26.     {
  27.         DataTable table = new DataTable();
  28.         table.Columns.Add(new DataColumn("ID"));
  29.         table.Columns.Add(new DataColumn("Name"));
  30.         table.Columns.Add(new DataColumn("UsuallyResults"));
  31.         table.Columns.Add(new DataColumn("ExamResults"));
  32.         foreach (GridViewRow row in GridView1.Rows)
  33.         {
  34.             DataRow sourseRow = table.NewRow();
  35.             sourseRow["ID"] = row.Cells[0].Text;
  36.             sourseRow["Name"] = ((TextBox)row.Cells[3].FindControl("txtName")).Text;
  37.             sourseRow["UsuallyResults"] = ((TextBox)row.Cells[4].FindControl("txtUsuallyResults")).Text;
  38.             sourseRow["ExamResults"] = ((TextBox)row.Cells[5].FindControl("txtExamResults")).Text;
  39.             table.Rows.Add(sourseRow);
  40.         }
  41.         return table;
  42.     }

  43.     protected void btnDeleteRow_Click(object sender, EventArgs e)
  44.     {
  45.         DataTable table = GetGridViewData();

  46.         foreach (GridViewRow row in GridView1.Rows)
  47.         {
  48.             if (((HtmlInputCheckBox)row.Cells[2].FindControl("chkRow")).Checked)
  49.             {
  50.                 foreach (DataRow dtRow in table.Rows)
  51.                 {
  52.                     if (dtRow["ID"].ToString() == row.Cells[0].Text)
  53.                     {
  54.                         table.Rows.Remove(dtRow);
  55.                         break;
  56.                     }
  57.                 }
  58.             }
  59.         }

  60.         GridView1.DataSource = table;
  61.         GridView1.DataBind();
  62.     }
复制代码

评分

参与人数 1金币 +500 收起 理由
JeffryLI + 500 很给力!

查看全部评分

回复 使用道具 举报
JeffryLI
葡萄城公司职员   /  发表于:2018-5-22 14:55:52
6#
非常感谢哈。

评分

参与人数 1满意度 +5 收起 理由
zhuyinyin_66 + 5

查看全部评分

请点击评分,对我5分评价,谢谢!

葡萄城控件服务团队
官方网站: https://www.grapecity.com.cn/developer
回复 使用道具 举报
zhuyinyin_66
高级会员   /  发表于:2018-5-22 16:34:57
7#

不客气,还得经常麻烦你呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部