菜鸟求助!C#能不能单独设置GridView某一个单元格里的字体和颜色

eccm12580 2014-09-28 02:54:13
单位做一个软件需要用GridView展示数据,表是做出来了,但是需求部门要求标题列项目字体不同,实在是不会,请教各位高手,有没有单独设置Gridview某一个单元格字体的办法?
...全文
3955 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaoao_2460950972 2014-09-29
  • 打赏
  • 举报
回复
设置GridView单元格的字体颜色 //GridView绑定数据显示 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="产品编号" ShowFooter="True" Width="624px" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" onpageindexchanging="GridView1_PageIndexChanging" PageSize="5"> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <Columns> <asp:TemplateField HeaderText="产品名称"> <ItemTemplate> <%#Highlightkeywords((string)Eval("产品"),this.tbSearch.Text.Trim()) %> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="单价" HeaderText="单价" SortExpression="单价" /> <asp:BoundField DataField="库存量" HeaderText="库存量" SortExpression="库存量" /> <asp:BoundField DataField="已订购量" HeaderText="已订购量" SortExpression="已订购量" /> <asp:TemplateField HeaderText="订货金额" SortExpression="订货金额"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("订货金额", "{0:c}") %>'></asp:Label> </EditItemTemplate> <FooterTemplate> <asp:Label ID="OrderTotalLabel" runat="server" Font-Underline="True" ForeColor="Red"></asp:Label> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("订货金额", "{0:c}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="订单日期"> <EditItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Eval("订单日期","{0:d}") %>'></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("订单日期","{0:d}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#C0FFC0" Font-Bold="True" ForeColor="Black" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> </asp:GridView> //设置标题列提供加载显示 public void DbBind() { string strCon = ConfigurationSettings.AppSettings["ConnectionString"]; string strsql = "SELECT 产品编号, 产品, 单价, 库存量, 已订购量, 订单日期,单价 * 已订购量 AS 订货金额 FROM tb_OrderForm"; sd.BindData(GridView1, strsql); } //显示绑定数据控件 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind(); DbBind(); } //搜索数据内容 protected void btnSearch_Click(object sender, EventArgs e) { string sqlstr = "select 产品编号, 产品, 单价, 库存量, 已订购量, 订单日期,单价 * 已订购量 AS 订货金额 from tb_OrderForm where 产品 like '%" + tbSearch.Text + "%'"; Highlightkeywords(sqlstr, this.tbSearch.Text.Trim()); DbBind(); } //GridView实现搜索关键字高亮显示 // 替换关键字为红色 // <param name="keycontent">原始内容</param> // <param name="k">关键字,支持多关键字</param> public string Highlightkeywords(string keycontent, string k) { string resultstr = keycontent; if (k == "") { return keycontent; } if (k.Trim().IndexOf(',') > 0) { string[] myArray = k.Split(','); for (int i = 0; i < myArray.Length; i++) { resultstr = resultstr.Replace(myArray[i].ToString(), "<span class='highlightTxtSearch'>" + myArray[i].ToString() + "</span>"); } return resultstr; } else { return resultstr.Replace(k, "<span class='highlightTxtSearch'>" + k + "</span>"); } }//字符关系其他设置(略)
eccm12580 2014-09-28
  • 打赏
  • 举报
回复
谢谢各位的提醒,基本上都完成了。
於黾 2014-09-28
  • 打赏
  • 举报
回复
gv.rows[4].cells[3].Style.Font=new Font("宋体",12); gv.rows[4].cells[3].Style.ForeColor=Color.Red;
OenAuth.Core 2014-09-28
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int j = 0; j < e.Row.Cells.Count; j++)
        {
            e.Row.Cells[j].Style.Add("BORDER-BOTTOM", "#aaccee 1px solid");
            e.Row.Cells[j].Style.Add("BORDER-RIGHT", "#aaccee 1px solid");
            e.Row.Cells[j].Style.Add("padding-left", "5px");
        }
    }
or

protected void Grid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int number;
        var highestCells = e.Row.Cells.Cast<TableCell>()
            .Where(tc => int.TryParse(tc.Text, out number))
            .OrderByDescending(c => int.Parse(c.Text));
        foreach(var cell in highestCells)
            cell.Font.Bold = true;
    }
}
eccm12580 2014-09-28
  • 打赏
  • 举报
回复
引用 4 楼 diaodiaop 的回复:

gv.rows[4].cells[3].backcolor=sys.drawing.color.red;
手写的...就是这个意思了..
按照这个代码,颜色是OK里,但是请教一下如何设置字的大小和字体。
eccm12580 2014-09-28
  • 打赏
  • 举报
回复
还要请教一下字体大小和对齐能不能设置
於黾 2014-09-28
  • 打赏
  • 举报
回复
dgv.Columns[0].HeaderCell.Sytle //设置某列标题单元格的样式 dgv.ColumnHeadersDefaultCellStyles //设置所有列标题的样式 可以继续点出Font,Color,WrapMode等属性
eccm12580 2014-09-28
  • 打赏
  • 举报
回复
不好意思,补充一下,是ASP.net
by_封爱 版主 2014-09-28
  • 打赏
  • 举报
回复

gv.rows[4].cells[3].backcolor=sys.drawing.color.red;
手写的...就是这个意思了..
guoweijun394 2014-09-28
  • 打赏
  • 举报
回复
补充:winform
guoweijun394 2014-09-28
  • 打赏
  • 举报
回复

           DataGridViewCellStyle style = new DataGridViewCellStyle();
           Font f = new Font("宋体",15,FontStyle.Bold) ;
           style.Font = f;
           style.ForeColor = Color.Red;
           column.DefaultCellStyle = style;  //column是你的datagrid 列 比如:DataGridViewTextBoxColumn


exception92 2014-09-28
  • 打赏
  • 举报
回复
asp.net??WinForm?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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