如何更改DataTable中单元格的前景色
本文关键字:前景色 单元格 何更改 DataTable | 更新日期: 2023-09-27 18:30:03
我正在尝试更改c#数据表中的文本颜色。现在我需要知道如何更改该表中特定单元格的前景(文本)颜色。
DataTable table = new DataTable();
//some code here
//lets say the cell is [0,0]
//change the foregroud color like this
table.columns[0].rows[0].forgroundColor ="red";
尝试:
table.Columns[0].Rows[0].ForegroundColor = Color.Red;
编辑
当我给出最初的答案时,我可能已经抢先一步了。我认为您正在尝试设置System.Data.DataTable
的颜色。这将永远不会起作用,因为此对象表示数据的集合,而不是实际的控件。
我认为你真正想要的是DataGrid
(取决于你使用的技术)。然后,在将DataTable
绑定到该控件后,您可以相应地设置该控件的样式。
我希望您使用的是数据表到网格,或者其他方面无法在数据表中使用颜色单元格。。
1.首先创建一个表格样式的
TableItemStyle tableCellStyle = new TableItemStyle();
tableCellStyle.ForeColor=System.Drawing.Color.Red;
现在将此样式应用于任何单元格对象
Cell.ApplyStyle(tableCellStyle);
现在我认为你可以根据你的手机管理你的代码了。
将任何样式应用于任何单元格,只有想象力才能限制
将"样式"列添加到数据表
DataSeta.BalanceSheetRow AssetsStockRow = (DataSeta.BalanceSheetRow)BalanceSheet.NewRow();
AssetsStockRow.CurrentAssetACC = "Stock Value";
AssetsStockRow.CurrentAssetAmt = Common.ConvertToDouble(stockValue.ToString()).ToString("00.00");
AssetsStockRow.style = "style='"color: gray;'"";
BalanceSheet.Rows.Add(AssetsStockRow);
<Columns>
<asp:TemplateField HeaderText="Sl. No.">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Current Assests" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<div <%#Eval("style") %>><%#Eval("CurrentAssetACC") %></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>