菜鸟求助!C#能不能单独设置GridView某一个单元格里的字体和颜色 DataGridViewCellStyle style = new DataGridViewCellStyle(); Font f = new Font("宋体",15,FontStyle.Bold) ; style.Font = f; style.ForeColor = Color.Red; column.DefaultCellStyle = style; //column是你的datagrid 列 比如:DataGridViewTextBoxColumn 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 gv.rows[4].cells[3].backcolor=sys.drawing.color.red;手写的...就是这个意思了.. 不好意思,补充一下,是ASP.net dgv.Columns[0].HeaderCell.Sytle //设置某列标题单元格的样式dgv.ColumnHeadersDefaultCellStyles //设置所有列标题的样式可以继续点出Font,Color,WrapMode等属性 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"); } }orprotected 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; }} 可以参考一下MSDN的:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx gv.rows[4].cells[3].Style.Font=new Font("宋体",12);gv.rows[4].cells[3].Style.ForeColor=Color.Red; 设置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>"); } }//字符关系其他设置(略) 尝试读取或写入受保护的内存。这通常指示其他内存已损坏 C#中如何让文本框中纯在固定字符,而且不影响内容输入,如货币符号 自定义控件如何取得当前值??? 如何遍历把数组中符合条件的数据放到另外数组? winform PNG 图片如何去白色背景 求助:熟悉Remoting的大虾请进 Excel-Dataset 为什么一直报错 保存xml已占用的问题!急 有dataset, 怎样绑定到combobox? c#可以用来游戏编程吗? 如何保存Word,Excel文件 请教c#的继承性
gv.rows[4].cells[3].backcolor=sys.drawing.color.red;
手写的...就是这个意思了..
dgv.ColumnHeadersDefaultCellStyles //设置所有列标题的样式
可以继续点出Font,Color,WrapMode等属性
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;
}
}
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
gv.rows[4].cells[3].Style.ForeColor=Color.Red;
//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>");
}
}//字符关系其他设置(略)