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;
    GridView2.DataBind();

    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">
    <Columns>
    <asp:ButtonField ButtonType = "Button" Text="Remove"
    CommandName="remove" />
    </Columns>
    </asp:GridView>

    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
    1. Advertising

  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)
    {
    //FillGrid();
    }
    }

    protected override void OnInit(EventArgs e)
    {
    FillGrid();
    base.OnInit(e);
    gvwGridView.RowCommand += new
    GridViewCommandEventHandler(gvwGridView_RowCommand);
    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.Controls.Add(lblTitleText);
    tdCell.Height = "30px";
    trRow.Cells.Add(tdCell);
    tblMain.Rows.Add(trRow);

    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.
    FromName("#aaaadd");
    gvwGridView.HeaderStyle.VerticalAlign = VerticalAlign.Bottom;
    gvwGridView.AlternatingRowStyle.BackColor = System.Drawing.Color.
    White;
    gvwGridView.HeaderStyle.Wrap = false;
    gvwGridView.AutoGenerateColumns = false;
    gvwGridView.EmptyDataText = "No Record Found.";

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

    trRow = new HtmlTableRow();
    tdCell = new HtmlTableCell();
    tdCell.Align = "Left";
    tdCell.Controls.Add(gvwGridView);
    trRow.Cells.Add(tdCell);
    tblMain.Rows.Add(trRow);
    PlaceHolder1.Controls.Add(tblMain);
    }

    private void CustomizeThisGrid(ref GridView MyGridView)
    {
    MyGridView.Columns.Clear();
    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";
    MyGridView.Columns.Add(col0);

    //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");
    //MyGridView.Columns.Add(TFld);

    col1.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col1.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col1.HeaderText = "Order ID";
    col1.DataField = "OrderID";
    MyGridView.Columns.Add(col1);

    col2.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col2.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col2.HeaderText = "EmployeeID";
    col2.DataField = "EmployeeID";
    MyGridView.Columns.Add(col2);

    col3.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col3.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col3.HeaderText = "OrderDate";
    col3.DataField = "OrderDate";
    MyGridView.Columns.Add(col3);

    col4.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    col4.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
    col4.HeaderText = "ShipCity";
    col4.DataField = "ShipCity";
    MyGridView.Columns.Add(col4);
    }

    protected void gvwGridView_RowCommand(object sender,
    GridViewCommandEventArgs e)
    {
    Context.Items.Add("Code", gvwGridView.Rows);
    Context.Items.Add("FrmMode","View");
    Server.Transfer("GridTest2.aspx");
    }

    #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);
    container.Controls.Add(l);
    }

    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";
    }
    }
    #endregion
    }
     
    senthilmurugan, May 8, 2007
    #2
    1. Advertising

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