|
|
|
สร้างหรือเพิ่ม columns ของ gridview จากทางฝั่งโค้ด(.aspx.cs) ได้หรือป่าว |
|
|
|
|
|
|
|
vb = https://www.thaicreate.com/dotnet/forum/043625.html
cs =
MyGridView.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyGridView.aspx.cs" Inherits="MyGridView" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
MyGridView.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class MyGridView : System.Web.UI.Page
{
private DataTable dataSource;
protected void Page_Init(object sender, EventArgs e)
{
dataSource = new DataTable();
dataSource = ViewState["dataSource"] != null ? (DataTable)ViewState["dataSource"] : GetDataSource();
}
protected void Page_Load(object sender, EventArgs e)
{
BoundField Index = new BoundField();
Index.HeaderText = "#";
Index.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
Index.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
Index.DataField = "ID";
BoundField MonthName = new BoundField();
MonthName.HeaderText = "MonthName";
MonthName.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
MonthName.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
MonthName.DataField = "Month";
CommandField DeleteButtom = new CommandField();
DeleteButtom.HeaderText = "Delete";
DeleteButtom.ButtonType = ButtonType.Image;
DeleteButtom.ShowEditButton = false;
DeleteButtom.ShowDeleteButton = true;
DeleteButtom.DeleteImageUrl = "~/images/delete-16x16.png";
DeleteButtom.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
DeleteButtom.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
GridView1.AutoGenerateColumns = false;
GridView1.DataKeyNames = new String[] { "ID" };
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
if (!IsPostBack)
{
GridView1.Columns.Add(Index);
GridView1.Columns.Add(MonthName);
GridView1.Columns.Add(DeleteButtom);
GridView1.DataSource = dataSource;
GridView1.DataBind();
}
ViewState["dataSource"] = dataSource;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton DeleteButton = (ImageButton)e.Row.Cells[2].Controls[0];
DeleteButton.OnClientClick = "javascript:return confirm('คุณต้องการลบข้อมูลนี้ใช่หรือไม่')";
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dataSource.Rows[((int)GridView1.DataKeys[e.RowIndex].Value) - 1].Delete();
dataSource.AcceptChanges();
GridView1.DataSource = dataSource;
GridView1.DataBind();
ViewState["dataSource"] = dataSource;
MessageBox.Show(GridView1.DataKeys[e.RowIndex].Value.ToString());
}
private DataTable GetDataSource()
{
DataTable Dt = new DataTable();
Dt.Columns.Add(new DataColumn("ID", typeof(int)));
Dt.Columns.Add(new DataColumn("Month", typeof(string)));
for (int i = 1; i <= 12; i++)
{
DataRow Dr = Dt.NewRow();
Dr["ID"] = i.ToString();
Dr["Month"] = DateTime.Parse(string.Format("1/{0}/2010", i.ToString())).ToString("MMMM");
Dt.Rows.Add(Dr);
}
return Dt;
}
}
|
|
|
|
|
Date :
2010-06-12 08:41:13 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณนะครับ
|
|
|
|
|
Date :
2010-06-14 14:13:16 |
By :
pordee |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จิ๊บ ๆ แค่นี้ เนี่ยน่ะ...จากน้องพี
|
|
|
|
|
Date :
2010-09-01 17:39:14 |
By :
หมีพู |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คอมเม้นบนเกรียนมาก อิอิ
|
|
|
|
|
Date :
2010-09-01 17:43:09 |
By :
เทเลวิซ..เทาๆ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คอมเม้นบน ทำให้รกบอร์ดคับ ^^
|
|
|
|
|
Date :
2010-09-01 17:48:20 |
By :
ใหย่ ณ บางฟิ้ว...วี |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คอมเม้นบนอ้วนนะคะ
|
|
|
|
|
Date :
2010-09-02 15:32:26 |
By :
คนที่ผอมกว่าคอมเม้นบน |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|