日期:2014-05-18 浏览次数:20460 次
public void Bind() { string conn = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("./data/english.mdb"); string cmd = "select id,title,type,dt from News"; OleDbConnection myconnection=new OleDbConnection (conn); OleDbDataAdapter da = new OleDbDataAdapter(cmd, conn); myconnection .Open (); DataSet ds = new DataSet(); da.Fill(ds, "News"); GridView1.DataSource = ds; //设置主键; GridView1.DataKeyNames = new string[] { "id" }; GridView1.DataBind(); //用“...”代替超长字符串,解决方法:数据绑定后过滤每一行即可 for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { DataRowView mydrv; string gIntro; if (GridView1.PageIndex == 0) { mydrv = ds.Tables["News"].DefaultView[i];//处理的表 gIntro = Convert.ToString(mydrv["title"]);//所要处理的字段 GridView1.Rows[i].Cells[1].Text = SubStr(gIntro, 5); } else { mydrv = ds.Tables["News"].DefaultView[i + (2 * GridView1.PageIndex)];//每页记录条数2; gIntro = Convert.ToString(mydrv["title"]); GridView1.Rows[i].Cells[1].Text = SubStr(gIntro, 5); } } myconnection .Close (); } //调用的方法: public string SubStr(string sString, int nLeng) { if (sString.Length <= nLeng) { return sString; } string sNewStr = sString.Substring(0, nLeng); sNewStr = sNewStr + "..."; return sNewStr; }
------解决方案--------------------
如何分页……偶也想问
------解决方案--------------------
再帮楼主问一个:
新闻列中如
asdfsadfffffff....
如何使当鼠标置于新闻列上方,显示类似于tooltip的内容为新闻标题全称.此功能如何实现?
------解决方案--------------------
楼上的代码多了点!!!
一般情况下在dataview控件RowDataBound事件中写这么一段代码应该可以解决问题了
string[] StrCol = new string[3];
if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
{
string Title = "";
Title = e.Item.Cells[1].Text;
if (Title.Length > 10)//大于10个长度
{
e.Item.Cells[1].Text = "<a href=\"news_show.aspx?id=" + this.DGBulletin.DataKeys[e.Item.ItemIndex] + ">" + Title.Substring(1, 10) + "...</a>";
}
else
e.Item.Cells[1].Text = "<a href=\"news_show.aspx?id=" + this.DGBulletin.DataKeys[e.Item.ItemIndex] + ">" + Title + "...</a>";