Creating GridView from array. Adding Button Field column?

Discussion in 'ASP .Net' started by JB, Mar 14, 2007.

  1. JB

    JB Guest

    I'm dynamically creating a Gridview object and filling it with
    contents from an ArrayList as follows:

    GridView2.AutoGenerateColumns = true;
    GridView2.DataSource = ArrayListObject;

    This works nicely, but i need to add a button field row onto it, as
    well as possibly a textbox and image field later.

    If I add it into the page source like this:

    <asp:GridView ID="GridView2" runat="server">
    <asp:ButtonField ButtonType = "Button" Text="Remove"
    CommandName="remove" />

    It works, but it displays the button as the first column, and i need
    it as the last column.

    Seems like a simple thing to do but its causing me grief.
    Can't seem to find a way to add a specific column in codebehind..

    Any ideas?
    JB, Mar 14, 2007
    1. Advertisements

  2. Hi I was also in the same situation. But I find the solution little bit. I
    am able to add Button. But I need to pass the Value of the third column to
    Next page.

    Code For You
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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.Drawing;
    using System.IO;

    public partial class GridTest1 : System.Web.UI.Page
    GridView gvwGridView;
    protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)

    protected override void OnInit(EventArgs e)
    gvwGridView.RowCommand += new
    Page.Load += new EventHandler(Page_Load);

    private void FillGrid()
    gvwGridView=new GridView();
    HtmlTable tblMain=new HtmlTable();
    HtmlTableRow trRow;
    HtmlTableCell tdCell;

    trRow = new HtmlTableRow();
    tdCell = new HtmlTableCell();

    Label lblTitleText=new Label();
    lblTitleText.Text = "Customer List";
    lblTitleText.Font.Bold = true;
    lblTitleText.BackColor = System.Drawing.Color.Navy;
    lblTitleText.ForeColor = System.Drawing.Color.White;
    tdCell.Height = "30px";

    gvwGridView.HorizontalAlign = HorizontalAlign.Left;
    gvwGridView.BackColor = System.Drawing.Color.FromName("#ccccff")
    gvwGridView.BorderColor = System.Drawing.Color.Black;
    gvwGridView.CellPadding = 3;
    gvwGridView.CellSpacing = 0;
    gvwGridView.PageSize = 10;
    gvwGridView.Font.Name = "Verdana";
    gvwGridView.Font.Size = 8;
    gvwGridView.HeaderStyle.Font.Size = 10;
    gvwGridView.HeaderStyle.BackColor = System.Drawing.Color.
    gvwGridView.HeaderStyle.VerticalAlign = VerticalAlign.Bottom;
    gvwGridView.AlternatingRowStyle.BackColor = System.Drawing.Color.
    gvwGridView.HeaderStyle.Wrap = false;
    gvwGridView.AutoGenerateColumns = false;
    gvwGridView.EmptyDataText = "No Record Found.";

    CustomizeThisGrid(ref gvwGridView);
    gvwGridView.DataSource = SqlDataSource1;

    trRow = new HtmlTableRow();
    tdCell = new HtmlTableCell();
    tdCell.Align = "Left";

    private void CustomizeThisGrid(ref GridView MyGridView)
    ButtonField col0 = new ButtonField();
    BoundField col1 = new BoundField();
    BoundField col2 = new BoundField();
    BoundField col3 = new BoundField();
    BoundField col4 = new BoundField();
    HyperLinkField Hyp1 = new HyperLinkField();
    TemplateField TFld = new TemplateField();
    //ButtonField col0 = new ButtonField();

    col0.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col0.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col0.HeaderText = "View"; ;
    col0.CommandName = "View";
    col0.ButtonType = ButtonType.Button;
    col0.ImageUrl = "~/Images/calendar.gif";
    col0.Text = "View";

    //Hyp1.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    //Hyp1.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    //Hyp1.HeaderText = "ID#";
    //Hyp1.DataTextField = "CustomerID";
    //Hyp1.NavigateUrl = "GridTest2.aspx";

    //TFld.HeaderText = "Select";
    //TFld.ItemTemplate = new CTemplateColumn("CustomerID");

    col1.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col1.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col1.HeaderText = "Order ID";
    col1.DataField = "OrderID";

    col2.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col2.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col2.HeaderText = "EmployeeID";
    col2.DataField = "EmployeeID";

    col3.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col3.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col3.HeaderText = "OrderDate";
    col3.DataField = "OrderDate";

    col4.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col4.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col4.HeaderText = "ShipCity";
    col4.DataField = "ShipCity";

    protected void gvwGridView_RowCommand(object sender,
    GridViewCommandEventArgs e)
    Context.Items.Add("Code", gvwGridView.Rows);

    #region "DataGridTemplateColumn-Part-II"
    public class CTemplateColumn : ITemplate
    private string colname;

    public CTemplateColumn(string cname)
    colname = cname;

    //must implement following method
    public void InstantiateIn(Control container)
    LinkButton l = new LinkButton();
    l.DataBinding += new EventHandler(this.OnDataBinding);

    public void OnDataBinding(object sender, EventArgs e)
    LinkButton l = (LinkButton)sender;
    GridViewRow GRow = (GridViewRow)l.NamingContainer;
    l.Text = DataBinder.Eval(GRow.DataItem, colname).ToString();
    l.CommandName = "GridTest2.aspx";
    senthilmurugan, May 8, 2007
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page