我在Gridview里表头的文字太长了,想换行,可是换行的时候换距又太大了,有没有缩小表头行距的方法?
只缩小表头行距,其他的行距不变!我的前台代码是这样的:<asp:GridViewID="GridView1"runat="server"AllowPaging="True...
只缩小表头行距,其他的行距不变!我的前台代码是这样的:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" Font-Size="12px" PageSize="8">
<Columns>
<asp:BoundField DataField="ID" HeaderText="id" Visible="False" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField HeaderText="项目名称<br/>(点击可编辑)">
<ItemTemplate>
<a href="companyquoteupdate.aspx?id=<%# Eval("ID") %>" style=" color:Black;"><%# Eval("ProjectName")%></a>
</ItemTemplate>
<ItemStyle ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:BoundField DataField="Unit" HeaderText="单位" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Wages" HeaderText="人工单价" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="secondMaterial" HeaderText="辅材单价" />
<asp:BoundField DataField="mainMaterial" HeaderText="主材单价" />
<asp:TemplateField HeaderText="备注(点击查看详细)">
<ItemTemplate>
<span>
<a href="companyquotenoteinfo.aspx?id=<%# Eval("ID") %>" style=" color:Black;"><%# GetText(Eval("Note").ToString(),20) %></a></ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="网上预约(点击查看详细)">
<ItemTemplate>
<a href="yuyueinfo.aspx?id=<%# Eval("ID") %>" ><%# Eval("number")%>条</a>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#E3EAEB" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#005599" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
pvldm95可能没理解我的意思,这是个表头,最好还是不要截取字段吧,要是要截取的话,我就不用写(点击编辑)了,不过还是谢谢你的回答。
gygh9891这样子做其实还是一样,我换行会换的,只是换行的时候行距太大了,那样子表头太难看了,我是想能不能缩写点 展开
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" Font-Size="12px" PageSize="8">
<Columns>
<asp:BoundField DataField="ID" HeaderText="id" Visible="False" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField HeaderText="项目名称<br/>(点击可编辑)">
<ItemTemplate>
<a href="companyquoteupdate.aspx?id=<%# Eval("ID") %>" style=" color:Black;"><%# Eval("ProjectName")%></a>
</ItemTemplate>
<ItemStyle ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:BoundField DataField="Unit" HeaderText="单位" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Wages" HeaderText="人工单价" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="secondMaterial" HeaderText="辅材单价" />
<asp:BoundField DataField="mainMaterial" HeaderText="主材单价" />
<asp:TemplateField HeaderText="备注(点击查看详细)">
<ItemTemplate>
<span>
<a href="companyquotenoteinfo.aspx?id=<%# Eval("ID") %>" style=" color:Black;"><%# GetText(Eval("Note").ToString(),20) %></a></ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="网上预约(点击查看详细)">
<ItemTemplate>
<a href="yuyueinfo.aspx?id=<%# Eval("ID") %>" ><%# Eval("number")%>条</a>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#E3EAEB" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#005599" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
pvldm95可能没理解我的意思,这是个表头,最好还是不要截取字段吧,要是要截取的话,我就不用写(点击编辑)了,不过还是谢谢你的回答。
gygh9891这样子做其实还是一样,我换行会换的,只是换行的时候行距太大了,那样子表头太难看了,我是想能不能缩写点 展开
2个回答
展开全部
首先是GridView 中的文本不自动换行,隐藏超出宽度部分,可以使用以下函数:
return (str.Length > num) ? str.Substring(0, num) + "..." : str;
int num 是你要截取的字符串的长度,然后把这个函数写在行绑定数据的事件中即可。
要是要使GridView里的内容自动换行的话,使用以下办法
在页面加入属性:style =" word-break :break-all ; word-wrap:break-word " (table或div 等的属性里)
或设置
protected void GridView1_DataRowBound(object o, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//设置要换行的模板列
e.Row.Cells[0].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
}
}
或者
如何实现:GridView 控件中显示的文本不自动换行,隐藏超出宽度部分
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="linkbNewsTitle" runat="server" Text='<%# Bind("ch_Remark") %>' ToolTip='<%# Bind("ch_Remark") %>'>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="300" />
</asp:TemplateField>
protected void gvNewsManage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((LinkButton)e.Row.FindControl("linkbNewsTitle") != null)
{
LinkButton linkbTitle = (LinkButton)e.Row.FindControl("linkbNewsTitle");
if (linkbTitle.Text.Length > 10)
{
linkbTitle.Text = linkbTitle.Text.Substring(0, 9) + "";
}
Label lblinknewID = (Label)e.Row.FindControl("ltgvnid");
// linkbTitle.PostBackUrl = "EditNewsContent.aspx?ID=" + lblinknewID.Text;
}
}
//==========================================
protected void gvNewsManage_RowDataBound(object sender, GridViewRowEventArgs e)
{
string strValue = e.Row.Cells[18].Text.ToString();
if (strValue != null)
{
if (strValue.Length > 9)
{
e.Row.Cells[18].Text = strValue.Substring(0, 9) + "";
}
}
}
return (str.Length > num) ? str.Substring(0, num) + "..." : str;
int num 是你要截取的字符串的长度,然后把这个函数写在行绑定数据的事件中即可。
要是要使GridView里的内容自动换行的话,使用以下办法
在页面加入属性:style =" word-break :break-all ; word-wrap:break-word " (table或div 等的属性里)
或设置
protected void GridView1_DataRowBound(object o, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//设置要换行的模板列
e.Row.Cells[0].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
}
}
或者
如何实现:GridView 控件中显示的文本不自动换行,隐藏超出宽度部分
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="linkbNewsTitle" runat="server" Text='<%# Bind("ch_Remark") %>' ToolTip='<%# Bind("ch_Remark") %>'>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="300" />
</asp:TemplateField>
protected void gvNewsManage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((LinkButton)e.Row.FindControl("linkbNewsTitle") != null)
{
LinkButton linkbTitle = (LinkButton)e.Row.FindControl("linkbNewsTitle");
if (linkbTitle.Text.Length > 10)
{
linkbTitle.Text = linkbTitle.Text.Substring(0, 9) + "";
}
Label lblinknewID = (Label)e.Row.FindControl("ltgvnid");
// linkbTitle.PostBackUrl = "EditNewsContent.aspx?ID=" + lblinknewID.Text;
}
}
//==========================================
protected void gvNewsManage_RowDataBound(object sender, GridViewRowEventArgs e)
{
string strValue = e.Row.Cells[18].Text.ToString();
if (strValue != null)
{
if (strValue.Length > 9)
{
e.Row.Cells[18].Text = strValue.Substring(0, 9) + "";
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询