简体   繁体   English

ASP.NET动态GridView创建

[英]ASP.NET Dynamic GridView Creation

How can I add an Edit column in this GridView dynamically? 如何在此GridView中动态添加“编辑”列?

替代文字

I have been able to create this GridView dynamically with the following code. 我已经能够使用以下代码动态创建此GridView。

The aspx file: aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView___Test._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>Untitled Page</title>
</head>

<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" Font-Names="Verdana" Font-Size="Small" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False">            
        </asp:GridView>

    </div>
    </form>
</body>

</html>

The code behind: 后面的代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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 Ice_Web_Portal.BO;

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

        private void CreateGridView()
        {
            DataTable dataTable = Book.GetBooksDataSet().Tables[0];

            foreach (DataColumn c in dataTable.Columns)
            {
                BoundField boundField = new BoundField();

                boundField.DataField = c.ColumnName;
                boundField.HeaderText = c.ColumnName;

                GridView1.Columns.Add(boundField);
            }

            GridView1.DataSource = dataTable;
            GridView1.DataBind();
        }
    }
}

Now please tell how to add some more code to add Edit column in this GridView dynamically. 现在,请介绍如何添加更多代码以在此GridView中动态添加“编辑”列。

out of the foreach, try 走出前面,尝试

CommandField c = new CommandField();
c.ShowEditButton = true;

c.Columns.Add(c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM