分别填充并绘制每个gridview单元格?
我有SQL表dbo.Clicks看起来像这样:分别填充并绘制每个gridview单元格?
ColNum Color RowNum Message
1 Gold 1 Text1
1 Black 2 Text2
2 Red 2 MoreText
2 Blue 3 TextX
我的存储过程返回这样这个相同的数据。这是基本datatable
:
Col1 Col2
-----------------------
Gold (null)
Black Red
(null) Blue
我填写每个单元RowDataBound
:
protected void GridViewClicks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
cell.BackColor = ConvertFromHexToColor(cell.Text);
}
}
}
此代码的工作,因为它填补了细胞与其对应的背景色。
现在的问题是,我还需要在每个单元格中显示我的sql表[dbo.Clicks]的内容。这就是我卡住的地方。
另一种方法是,如果我使用示例数据,每个数据表单元都包含颜色和文本,就像这样。然后我解析它:
Col1 Col2
Gold/Text1 (null)
Black/Text2 Red/MoreText
(null) Blue/TextX
但我在想,必须有一个更优雅的方式来做到这一点。对我而言,这个解决方案非常难看。
我的GridView看起来是这样的:
<asp:GridView ID="GridViewClicks" runat="server" ShowHeader="False" onrowdatabound="GridViewClicks_RowDataBound">
</asp:GridView>
感谢。
可以在OnRowDataBound
事件中使用DataRowView
创建的GridView并从一行中获取单个值并应用他们到特定的细胞。
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the dataitem back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//use the data from the datarowview to specify color and contents for specific cells
e.Row.Cells[0].BackColor = Color.FromName(row["Color"].ToString());
e.Row.Cells[0].Text = row["RowNum"].ToString();
e.Row.Cells[1].BackColor = Color.FromName(row["Color"].ToString());
e.Row.Cells[1].Text = row["Message"].ToString();
}
UPDATE
如果在GridView有3列,和数据源6,交替文字/色彩值,你可以创建一个循环
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the dataitem back to a datarowview
DataRowView drv = e.Row.DataItem as DataRowView;
//loop all the items in the datarowview (not equal to columns in grid)
for (int i = 0; i < drv.Row.ItemArray.Length; i++)
{
//check if it is an uneven column
if (i % 2 == 0)
{
e.Row.Cells[i/2].Text = drv[i].ToString();
}
else
{
e.Row.Cells[i/2].BackColor = Color.FromName(drv[i].ToString());
}
}
}
您可以使用带有标签的模板字段并使用Eval分配样式属性。
public class SomeData
{
public string Data1 { get; set; }
public string Data2 { get; set; }
public string Color1 { get; set; }
public string Color2 { get; set; }
}
List<SomeData> lstData = new List<SomeData>()
{
new SomeData() {Data1 = "AAA", Color1 = "Red", Data2 = "ZZZ", Color2 = "Green"},
new SomeData() {Data1 = "BBB", Color1 = "Blue", Data2 = "PPP", Color2 = "Gold"},
new SomeData() {Data1 = "CCC", Color1 = "Red", Data2 = "ZZZ", Color2 = "Yellow"},
};
grdView.DataSource = lstData;
grdView.DataBind();
与模板场像下面
<asp:GridView runat="server" ID="grdView" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Data1">
<ItemTemplate>
<asp:Label ID="lblColor1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Data1") %>' style= <%# String.Concat("background-color:",Eval("Color1")) %> ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data2">
<ItemTemplate>
<asp:Label ID="lblColor2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Data2") %>' style= <%# String.Concat("background-color:",Eval("Color2")) %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
此位会做的伎俩为您
style= <%# String.Concat("background-color:",Eval("Color2")) %>
感谢您的答复。这将工作与SQL服务器表或数据表?另外,这是否考虑到我不知道会有多少列? – rbhat
它可以处理来自任何数据源的数据。您需要知道列数,因为gridview具有需要为数据库表中的每个列指定的模板字段。 。 – Hakunamatata
感谢您的回复。我注意到你正在为“细胞”设置一个固定值。我不知道数据表有多少列。你的答案仍然有效吗? – rbhat
这取决于从数据源接收列和颜色的方式。如果它是'文本,颜色,文本,颜色'等,你可以在rowdatabound事件循环,每个单元格的颜色-1' – VDWWD
我已经更新了我的答案一个简单的例子。 – VDWWD