如何动态添加此GridView中的编辑列?
我已经能够使用以下代码动态创建此GridView。
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>
背后的代码:
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();
}
}
}
现在请告诉我们如何添加更多代码以动态添加此GridView中的Edit列。
答案 0 :(得分:1)
脱离了foreach,试试
CommandField c = new CommandField();
c.ShowEditButton = true;
c.Columns.Add(c);