- 爱易网页
-
ASP.NET教程
- 关于GridView的列有关问题!高手帮忙啊
日期:2014-05-19 浏览次数:20397 次
关于GridView的列问题!高手帮忙啊!在线等~~~
我用代码直接写的绑定程序,没在属性里设置,现在我想增加一个链接字段,并且需要给另一个页传值,除了在属性里设置,谁知道怎么在代码里添加这个连接字段列啊!~!
小弟急啊...大哥门帮忙啊
绑定代码:
string SqlConn;
SqlConn = ConfigurationManager.AppSettings[ "SqlConnection "];
SqlConnection myConn = new SqlConnection(SqlConn);
myConn.Open();
SqlDataAdapter myAda1 = new SqlDataAdapter( "select id as '编号 ',shengfen as '省份 ' from B2B_Quyu order by id asc ", myConn);
DataSet myds1 = new DataSet();
myAda1.Fill(myds1);
this.GridView1.DataSource = myds1.Tables[0];
this.GridView1.DataBind();
myAda1.Dispose();
myConn.Close();
小弟在线等,高手大哥给解决一下!~
------解决方案--------------------
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.createBind();
}
this.soking();
}
private void soking()
{
SqlDataSource sqlds = new SqlDataSource(); //创建SqlDataSource
sqlds.ConnectionString= ConfigurationManager.ConnectionStrings[ "sqlLink "].ConnectionString; //创建链接
sqlds.SelectCommand = ( "select id as '编号 ',shengfen as '省份 ' from B2B_Quyu order by id asc ");
sqlds.ID = "idName ";
sqlds.DataSourceMode = SqlDataSourceMode.DataSet;
Page.Controls.Add(sqlds);
GridView1.DataSourceID = sqlds.ID;
}
private void createBind()
{
HyperLinkField HLF = new HyperLinkField(); //创建HyperLinkField (这个不用我多说了吧)
HLF.DataTextField = "shengfen "; //把省份作为显示的字段, 以超链接方式; (根据你的需要)
//下面的两行代码. 自己看下就知道了. id为传递的值. 通常是主键
HLF.DataNavigateUrlFields = new string[] { "id " };
HLF.DataNavigateUrlFormatString = "king.aspx?title={0} ";
//下面的不是很重要. 有很多的属性. 没有加.比如表头样式什么的
HLF.ItemStyle.Wrap = false;
HLF.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
GridView1.Columns.Add(HLF); //最后就是加入到GridView1
}
}