c#代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            BindGrid();
        }
    }

    private DataTable ReadGridView()
    {
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new DataColumn("ProductID", typeof(string)));
        dt.Columns.Add(new DataColumn("ProductName", typeof(string)));
        dt.Columns.Add(new DataColumn("CategoryID", typeof(string)));
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            dr = dt.NewRow();
            dr[0] = this.GridView1.Rows[i].Cells[0].Text.Trim();
            dr[1] = this.GridView1.Rows[i].Cells[1].Text.Trim();
            dr[2] = this.GridView1.Rows[i].Cells[2].Text.Trim();
            dt.Rows.Add(dr);
        }
        return dt;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = ReadGridView();
        //this.GridView1.DataSource = dt;
        //this.GridView1.DataBind();
        DataRow row = dt.NewRow();
        row.ItemArray = new object[] { "oec2003","oec2003","oec2003" };
        dt.Rows.InsertAt(row, 0);
        dt.AcceptChanges();
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }

    private void BindGrid()
    {
        string str = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
        using (SqlConnection con = new SqlConnection(str))
        {
            SqlCommand cmd = new SqlCommand("SELECT top 1  [ProductID], [ProductName], [CategoryID] FROM [Products]", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            this.GridView1.DataSource = ds.Tables[0].DefaultView;
            this.GridView1.DataBind();
            sda.Dispose();
            ds.Dispose();
        }

    }
}

html代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID">
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
            </Columns>
        </asp:GridView> 
    </div>
    </form>
</body>
</html>

数据库连接字串

<connectionStrings>
        <add name="NorthwindConnectionString" connectionString="Data Source=FENGWEI;Initial Catalog=Northwind;User ID=sa;Password=1234" providerName="System.Data.SqlClient"/>
    </connectionStrings>

AddRow.rar

转载于:https://www.cnblogs.com/JemBai/archive/2008/09/01/1281250.html

动态添加gridview行【转:http://www.cnblogs.com/oec2003/articles/1069796.html】相关推荐

  1. DataGridView动态添加新行的两种方法

    简单介绍如何为DataGridView控件动态添加新行的两种方 法: 方法一: int index=this.dataGridView1.Rows.Add(); this.dataGridView1. ...

  2. 转——C# DataGridView控件 动态添加新行

    DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行, ...

  3. ASP.NET 2.0 中动态添加 GridView 模板列

      ASP.NET 2.0 中动态添加 GridView 模板列的例子 动态添加列,关键是实现 ITemplate.InstantiateIn 方法.下面是一个添加 GridView 模板列的例子. ...

  4. php 动态添加表格行,jQuery给html表格动态添加行方法总结

    这次给大家带来jQuery给html表格动态添加行方法总结,jQuery给html表格动态添加行的注意事项有哪些,下面就是实战案例,一起来看一下. 本文实例讲述了jQuery实现html表格动态添加新 ...

  5. antd table合并行或者列(动态添加合并行、列)

    antd table合并行或者列(动态添加合并行.列) 表头只支持列合并,使用 column 里的 colSpan 进行设置. 表格支持行/列合并,使用 render 里的单元格属性 colSpan ...

  6. ASP.NET 2.0 中动态添加 GridView 模板列的...

    framespacing="0" src="http://what.yesky.com/by/ztdh.htm" frameborder="0&quo ...

  7. C# DataGridView控件动态添加新行

    目录 方法一: 方法二: ① DataGridView 取得或者修改当前单元格的内容: ② DataGridView 设定单元格只读: ③ DataGridView 不显示最下面的新行: ④ Data ...

  8. pytest中pytest_addoption用法 动态添加命令行参数 (钩子函数)

    考虑场景: 我们的自动化用例需要支持在不同测试环境运行,有时候在dev环境运行,有时候在test环境运行: 有时候需要根据某个参数不同的参数值,执行不同的业务逻辑: 上面的场景我们都可以通过" ...

  9. js如何动态添加表格行

    一.总结 方法一:添加可通过在table的innerHTML属性中添加tr和td来实现 方法二:通过table的属性insertRow(),deleteRow()来实现 方法一和方法二对比: 二.js ...

  10. Asp.net(asp,jsp)+JavaScript动态实现添加数据行

    在应用程序的开发中,有些输入信息是动态的,比如我们要注册一个员工的工作经历,比如下图 如果做成死的,只能填写三个,如果是四个呢?或者更多呢,那不是添加不上去了吗,所以这样固然不好,我们可以用动态添加表 ...

最新文章

  1. 有什么事是你当了程序员才知道的? | 每日趣闻
  2. 营销型网站建设需要注意哪些问题?
  3. 隐藏html表格边框显示,table显示所有边框 table的边框怎么显示不出来
  4. Windows Server 2012 新特性:IPAM的配置
  5. org.tinygroup.databasebuinstaller-数据库结构及元数据自动创建
  6. 【转】Android 音量键+电源键 截屏代码小结
  7. C/C++初学者快速提升?
  8. NLP论文 -《Distributed Representations of Sentences and Documents》-句子和文档的分布式表示学习
  9. 基于JAVA+SpringMVC+Mybatis+MYSQL的家庭理财管理系统
  10. 多线程矩阵乘法运算 c++
  11. 阶段3 1.Mybatis_02.Mybatis入门案例_1.mybatis的入门
  12. 文档数据库-MongoDB
  13. 佛山科学技术学院计算机科学与技术李欣华,第七届国务院学位委员会中文学科评议组2019年度 扩大会议在佛山科学技术学院召开...
  14. hhkb java_为什么知乎上这么多人推荐 HHKB,却不反复强调说该键盘不适合大多数程序员?...
  15. c# Excel 添加组合Range区域,方便选择Select()删除 Delete()
  16. 让你实现财富自由,从此不再缺资金
  17. OKR 结果思维:你如何变得以结果为导向?(第三部分)
  18. 戴尔dell PowerEdge R440安装redhat操作系统
  19. 如何使用《背景音乐提取器》提取歌曲中的背景音乐
  20. 港科夜闻|香港科大再获[商科]评审全港第一!

热门文章

  1. 组态王历史记录用access_Access数据库与表在 组态王中使用.ppt
  2. 【43】学习处理模版化基类内的名称
  3. PostgreSQL 10.1 手册_部分 II. SQL 语言_第 10 章 类型转换_10.5. UNION、CASE和相关结构...
  4. 23种设计模式(6):模版方法模式
  5. 医学遗传学词汇英语术语英文(Glossary) 5
  6. 成为Oracle 10g DBA之路
  7. 千万不能返回局部变量的引用
  8. 【Java与智能设备】4_1 Activity的创建和跳转
  9. python模拟火车订票系统代码_100行Python代码实现自动抢火车票
  10. c语言 指针 地址溢出,堆栈溢出,指针在C中