How can I add an Edit column in this GridView dynamically?
I have been able to create this GridView dynamically with the following code.
The aspx file:
<%@ 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.
From stackoverflow
-
out of the foreach, try
CommandField c = new CommandField(); c.ShowEditButton = true; c.Columns.Add(c);
JMSA : GridView1.Columns.Add(c);michele : yep...sorry for the mistake!
0 comments:
Post a Comment