環境
VS2013
ASP.NET 4.0
GridView怎麼做刪除鈕與編輯鈕.
要做之前,我們要先拿到點下去的是哪一列.
也要知道點到的是刪除鈕還是編輯鈕.
HeaderText : ID
DataField : ID
Button Type : Image
CommandName : xxx 這樣就知道點到的是哪一顆按鈕了.
HeaderText : xxx
ImageUrl :
Button Type : Image
CommandName : 000 這樣就知道點到的是哪一顆按鈕了.
HeaderText : 000
ImageUrl :
RowCommd :
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "xxx")
{
int index = Convert.ToInt32(e.CommandArgument);//get點到的那一列
Button1.Text = "你按到xxx了:"+GridView1 .Rows[index].Cells[0].Text.ToString().Trim() ;//取點到那列的某個值.
}
if (e.CommandName == "000")
{
int index = Convert.ToInt32(e.CommandArgument);
Button1.Text = "你按到000了:" + GridView1.Rows[index].Cells[0].Text.ToString().Trim(); ;
}
}
protected void Page_Load(object sender, EventArgs e)
{
List<OrderItem> dataSource = new OrderItemData().DataSource;
this.GridView1.DataSource = dataSource;
this.GridView1.DataBind();
}
/// <summary>
/// 訂單項目資料
/// </summary>
public struct OrderItem
{
/// <summary>
/// 設定或取得訂單編號
/// </summary>
public Guid ID { set; get; }
/// <summary>
/// 設定或取得產品ID
/// </summary>
public Guid ItemID { set; get; }
/// <summary>
/// 設定或取得購買數量
/// </summary>
public int Count { set; get; }
/// <summary>
/// 設定或取得產品名稱
/// </summary>
public string ItemName { set; get; }
}
/// <summary>
/// 處理訂單項目的資料類別
/// </summary>
public class OrderItemData
{
/// <summary>
/// 取得模擬資料來源
/// </summary>
public List<OrderItem> DataSource
{
get
{
Guid itemId_1 = Guid.NewGuid(), itemId_2 = Guid.NewGuid(), itemId_3 = Guid.NewGuid(), itemId_4 = Guid.NewGuid();//隨機建立四個產品Id
List<OrderItem> result = new List<OrderItem>();//結果
Guid orderId_1 = new Guid("CB843080-755C-4F42-AE2B-A79DE62F503E");//第一筆訂單編號
result.Add(new OrderItem() { ID = orderId_1, ItemID = itemId_1, ItemName = "腳踏車", Count = 2 });
result.Add(new OrderItem() { ID = orderId_1, ItemID = itemId_2, ItemName = "暖氣", Count = 5 });
result.Add(new OrderItem() { ID = orderId_1, ItemID = itemId_3, ItemName = "肥皂", Count = 10 });
Guid orderId_2 = new Guid("FE390D81-BDF9-49C8-BD26-C51043FB6F0C");//第二筆訂單編號
result.Add(new OrderItem() { ID = orderId_2, ItemID = itemId_2, ItemName = "暖氣", Count = 1 });
result.Add(new OrderItem() { ID = orderId_2, ItemID = itemId_3, ItemName = "肥皂", Count = 7 });
result.Add(new OrderItem() { ID = orderId_2, ItemID = itemId_4, ItemName = "滑鼠", Count = 3 });
return result;
}
}
}
ref
https://dotblogs.com.tw/antony77/2011/04/28/23806