大家好,本贴内容转自:http://bbs.csdn.net/topics/250047480
Demo1:
前台代码:
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>GridView2 Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID = "grdView2" runat = "server" AutoGenerateColumns="False" > <Columns> <asp:HyperLinkField DataNavigateUrlFields="WebSite" DataNavigateUrlFormatString="http://www.{0}.com" DataTextField="WebSite" HeaderText="WebSite"/> <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> </Columns> </asp:GridView> </div> </form> </body> </html>
后台代码:
public partial class GridView2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string[,] siteurl = { {"google","guge","America"}, {"baidu","baidu","China"}, {"yahoo","yahu", "America"} }; // use grdView2 BoundGV2(siteurl); } # region Methods for grdView2 void BoundGV2(string[,] siteurl) { this.grdView2.DataSource = GetTable2(siteurl); this.grdView2.DataBind(); } DataTable GetTable2(string[,] siteurl) { DataTable dt = new DataTable(); dt.Columns.Add("WebSite", typeof(string)); dt.Columns.Add("Description", typeof(string)); dt.Columns.Add("Country", typeof(string)); for (int i = 0; i < siteurl.GetLength(0); i++) { int j = 0; DataRow row = dt.NewRow(); row["WebSite"] = siteurl[i, j++]; row["Description"] = siteurl[i, j++]; row["Country"] = siteurl[i, j]; dt.Rows.Add(row); } return dt; } #endregion }
Demo2:
前台代码:
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>GridView3 Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID = "grd" runat = "server" > </asp:GridView> </div> </form> </body> </html>
后台代码:
public partial class GridView3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string[,] siteurl = { {"google","guge","America"}, {"baidu","baidu","China"}, {"yahoo","yahu", "America"} }; BoundGV3(siteurl); } void BoundGV3(string[,] siteurl) { this.grd.AutoGenerateColumns = false; HyperLinkField hlf = new HyperLinkField(); string[] str = {"WebSite","Describe","Country"}; hlf.DataNavigateUrlFields = str ; hlf.DataNavigateUrlFormatString = "http://www.{0}.com"; hlf.DataTextField = "WebSite"; hlf.HeaderText = "Site"; this.grd.Columns.Add(hlf); BoundField bfDescribe = new BoundField(); bfDescribe.DataField = "Describe"; bfDescribe.HeaderText = "Description"; this.grd.Columns.Add(bfDescribe); BoundField bfCountry = new BoundField(); bfCountry.DataField = "Country"; bfCountry.HeaderText = "Country"; this.grd.Columns.Add(bfCountry); this.grd.DataSource = GetTable3(siteurl); this.grd.DataBind(); } DataTable GetTable3(string[,] siteurl) { DataTable dt = new DataTable(); dt.Columns.Add("WebSite",typeof (string)); dt.Columns.Add("Describe",typeof (string)); dt.Columns.Add("Country",typeof (string )); for (int iRow = 0; iRow < siteurl.GetLength(0); iRow++) { int j = 0; DataRow row = dt.NewRow(); row["WebSite"] = siteurl[iRow, j++]; row["Describe"] = siteurl[iRow, j++]; row["Country"] = siteurl[iRow, j]; dt.Rows.Add(row); } return dt; } }
请支持原作者:Jueyoung 的总结,非常感谢!