日期:2014-05-19  浏览次数:20862 次

如何在GridView後面添加一"詳細..."列?
這是   我的方法:
                this.GridView1.Columns.Clear();

                System.Web.UI.WebControls.HyperLinkField   lnkField   =   new   HyperLinkField();
                string[]   NavigateUrls   ={   "ID "   };
                lnkField.HeaderText   =   "詳細 ";
                lnkField.DataNavigateUrlFields   =   NavigateUrls;
                lnkField.DataNavigateUrlFormatString   =   "BookDetail.aspx?BookID={0} ";
                lnkField.Text= "詳細... ";
                this.GridView1.Columns.Add(lnkField);

但隻能加在每一行前第一列,怎麼將它加到每一行最後一列呢?
另問一個問題
怎麼給每一行加一個CheckBox用來選擇作用呢?

------解决方案--------------------
GridView1.Columns.Add方法就是将DataControlField加入集合的最后:

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt;
DataColumn col;
DataRow dr;

dt = new DataTable();

col = new DataColumn();
col.ColumnName = "id ";
dt.Columns.Add(col);

for (int i = 1; i < 5; i++)
{
dr = dt.NewRow();
dr[0] = i;

dt.Rows.Add(dr);
}

System.Web.UI.WebControls.HyperLinkField lnkField;
string[] NavigateUrls ={ "ID " };

lnkField = new HyperLinkField();
lnkField.HeaderText = "詳細1 ";
lnkField.DataNavigateUrlFields = NavigateUrls;
lnkField.DataNavigateUrlFormatString = "BookDetail.aspx?BookID={0} ";
lnkField.Text = "詳細1... ";
this.GridView1.Columns.Add(lnkField);

lnkField = new HyperLinkField();

lnkField.HeaderText = "詳細2 ";
lnkField.DataNavigateUrlFields = NavigateUrls;
lnkField.DataNavigateUrlFormatString = "BookDetail.aspx?BookID={0} ";
lnkField.Text = "詳細2... ";
this.GridView1.Columns.Add(lnkField);

GridView1.AutoGenerateColumns = false;
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}