截取文章标题长度

小豪 2011-05-22 03:24:40

public static DataTable GetTop8News()
{
string sql = "select top 8 * from T_News where newID=1 order by newsTime desc ";
DataTable dt = DataHelp.ExcuteSqlReturnTable(sql);
return dt;
}

SQLHelp

public static DataTable ExcuteSqlReturnTable(string sql)
{
try
{
SqlCommand command = new SqlCommand(sql, connection);
SqlDataAdapter adpater = new SqlDataAdapter(command);
DataTable dt = new DataTable();
adpater.Fill(dt);
return dt;
}
catch (Exception ex)
{
return null;
}
}

多于10个字符串的用.....表示,求解~
...全文
63 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zell419 2011-05-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 forevernome 的回复:]

引用 2 楼 zhizouxiao 的回复:

C# code
protected void gv_Test_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType==DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.leng……
……
[/Quote]
你可以在绑定的时候 用个后台方法处理下 。
4楼直接从sql弄也可以 。
一切为了你 2011-05-22
  • 打赏
  • 举报
回复
string sql = "select top 8 * ,SUBSTRING(新闻标题字段,1,10) as 别名 from T_News where newID=1 order by newsTime desc ";

在程序绑定就用 别名
小豪 2011-05-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhizouxiao 的回复:]

C# code
protected void gv_Test_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType==DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.leng……
[/Quote]
补充下这个是cs里面的,怎样将组合它们,我刚学,不太懂

public void NewsDataGridViewBind()
{
DataTable dt = NewBusiness.GetTop8News();
GridViewNew.DataSource=dt;//GridViewNew是GridView的ID
GridViewNew.DataBind();
}
zhizouxiao 2011-05-22
  • 打赏
  • 举报
回复
 protected void gv_Test_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType==DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.length>10)
{
e.Row.Cells[0].Text=e.Row.Cells[0].Text.Substring(1, 10)+"……";
}
}
}

这是gridview的
zhizouxiao 2011-05-22
  • 打赏
  • 举报
回复
你应该把返回的datatable绑定到某个控件吧,比如repeater、gridview
然后
 protected void gv_Test_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType==DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.length>100)
{
e.Row.Cells[0].Text=e.Row.Cells[0].Text.Substring(1, 100)+"……";
}
}
}

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧