473,378 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Creating GridView from array. Adding Button Field column?

JB
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?

Mar 14 '07 #1
1 4337
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
}
May 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: GL | last post by:
Hi, Is there a way to add a field to an existing table using a query of some sort (without needing to manually add a field to the table). I know how to do it with a make table query, but I have...
10
by: Tor Inge Rislaa | last post by:
Creating Control Array How to create an array of buttons, with common procedures based on the index of the control. How would this Example from VB 6.0 be in VB.NET? Private Sub...
2
by: Jasmine | last post by:
Hi, how can I set up the javascript for the button field in the grid view, so that when the user click the button, a pop up window will be opened, I mean where could I add the "window.open "url"...
1
by: Mike P | last post by:
How do you add a confirm delete pop-up for a button field? I've managed to do this for a link button, where you can refer to the button id, but there is no id to refer to for a button field. ...
0
by: shapper | last post by:
Hello, I added, at runtime, a button field of type link to a gridview. Now I need url of that button, when pressed, to be as follows: "news.aspx?id=" & channel "Channel" is the first...
5
by: bbawa1 | last post by:
Hi, I have a button field in GridView. It shows the ID. When I click on it should grab the id and open new ASpx page based on that ID. How can I do that. Thanks in advance
0
by: hellboss | last post by:
Hi ! Im using Asp.net vs05, rite now iam working with a grid view where i map the column values from a database to the Button Field's DataTextfield, example "Empno" , The problem is that !...
4
by: nico3334 | last post by:
I'm not that familiar with arrays so I'll try to explain my problem as clear as possible. I have 2 text boxes in VB where users are basically allowed to choose a date range up to 12 months. The...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.