Hi Michael,
I've done some research and found out that if we're using
AutoGenerateColumns, then there's no way to add our columns after those
generated columns since we cann't find an appropriate event to add our code.
There are two workarounds:
1) Don't use AutoGenerateColumns = true, use manually configured columns.
This way, you can simply add your columns in Page_Load.
2) Create a custom GridView, override the CreateColumns method to add your
own columns:
public class Class1 : GridView
{
protected override System.Collections.ICollection
CreateColumns(PagedDataSource dataSource, bool useDataSource)
{
ArrayList list = new ArrayList(base.CreateColumns(dataSource,
useDataSource));
BoundField nameColumn = new BoundField();
nameColumn.DataField = "au_lname";
nameColumn.HeaderText = "Person Name";
TemplateField ckhColumn = new TemplateField();
ckhColumn.HeaderTemplate = new
GridViewTemplate(ListItemType.Header, "CheckBox Column");
ckhColumn.ItemTemplate = new
GridViewTemplate(ListItemType.Item, "some data");
list.Add(ckhColumn);
list.Add(nameColumn);
return list;
}
}
(The GridViewTemplate is a custom ITemplate that adds some user defined
controls which is from
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=88)
public class GridViewTemplate : System.Web.UI.Page, ITemplate
{
ListItemType templateType;
string columnName;
public GridViewTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
LinkButton lb = new LinkButton();
CheckBox ckh = new CheckBox();
TextBox tb1 = new TextBox();
switch (templateType)
{
case ListItemType.Header:
lc.Text = "<B>" + columnName + "</B>";
lb.Text = "Edit";
lb.CommandName = "EditButton";
container.Controls.Add(lb);
container.Controls.Add(lc);
break;
case ListItemType.Item:
container.Controls.Add(tb1);
container.Controls.Add(ckh);
break;
case ListItemType.EditItem:
TextBox tb = new TextBox();
tb.Text = "";
container.Controls.Add(tb);
break;
case ListItemType.Footer:
lc.Text = "<I>" + columnName + "</I>";
container.Controls.Add(lc);
break;
}
}
}
Hope this helps.
Regards,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.