C# 是否分别填充和绘制每个gridview单元格?
我有sql表dbo。单击如下所示:C# 是否分别填充和绘制每个gridview单元格?,c#,asp.net,visual-studio-2010,gridview,C#,Asp.net,Visual Studio 2010,Gridview,我有sql表dbo。单击如下所示: ColNum Color RowNum Message 1 Gold 1 Text1 1 Black 2 Text2 2 Red 2 MoreText 2 Blue 3
ColNum Color RowNum Message
1 Gold 1 Text1
1 Black 2 Text2
2 Red 2 MoreText
2 Blue 3 TextX
<asp:GridView ID="GridViewClicks" runat="server" ShowHeader="False" onrowdatabound="GridViewClicks_RowDataBound">
</asp:GridView>
我的存储过程像这样返回相同的数据。这是基本的数据表
:
Col1 Col2
-----------------------
Gold (null)
Black Red
(null) Blue
我在行数据绑定中填充每个单元格
:
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]的内容。这就是我被困的地方
另一种方法是,如果使用示例数据,则每个datatable单元格都包含颜色和文本,类似于这样。然后我解析它:
Col1 Col2
Gold/Text1 (null)
Black/Text2 Red/MoreText
(null) Blue/TextX
但我认为必须有一种更优雅的方式来做这件事。对我来说,这个解决方案相当丑陋
我的gridview如下所示:
ColNum Color RowNum Message
1 Gold 1 Text1
1 Black 2 Text2
2 Red 2 MoreText
2 Blue 3 TextX
<asp:GridView ID="GridViewClicks" runat="server" ShowHeader="False" onrowdatabound="GridViewClicks_RowDataBound">
</asp:GridView>
谢谢。您可以使用带有标签的模板字段,并使用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();
公共类SomeData
{
公共字符串Data1{get;set;}
公共字符串Data2{get;set;}
公共字符串Color1{get;set;}
公共字符串Color2{get;set;}
}
List lstData=新列表()
{
新的SomeData(){Data1=“AAA”,Color1=“Red”,Data2=“ZZZ”,Color2=“Green”},
新建SomeData(){Data1=“BBB”,Color1=“Blue”,Data2=“PPP”,Color2=“Gold”},
新的SomeData(){Data1=“CCC”,Color1=“Red”,Data2=“ZZZ”,Color2=“Yellow”},
};
grdView.DataSource=lstData;
grdView.DataBind();
使用模板字段创建Gridview,如下所示
<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>
您可以使用带有标签的模板字段,并使用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();
公共类SomeData
{
公共字符串Data1{get;set;}
公共字符串Data2{get;set;}
公共字符串Color1{get;set;}
公共字符串Color2{get;set;}
}
List lstData=新列表()
{
新的SomeData(){Data1=“AAA”,Color1=“Red”,Data2=“ZZZ”,Color2=“Green”},
新建SomeData(){Data1=“BBB”,Color1=“Blue”,Data2=“PPP”,Color2=“Gold”},
新的SomeData(){Data1=“CCC”,Color1=“Red”,Data2=“ZZZ”,Color2=“Yellow”},
};
grdView.DataSource=lstData;
grdView.DataBind();
使用模板字段创建Gridview,如下所示
<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>
您可以在OnRowDataBound
事件中使用DataRowView
,从行中获取单个值并将其应用于特定单元格
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();
}
更新
如果GridView有3列,DataSource有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());
}
}
}
if(e.Row.RowType==DataControlRowType.DataRow)
{
//将dataitem强制转换回datarowview
DataRowView drv=e.Row.DataItem作为DataRowView;
//循环datarowview中的所有项(不等于网格中的列)
for(int i=0;i
您可以在OnRowDataBound
事件中使用DataRowView
,从行中获取单个值并将其应用于特定单元格
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();
}
更新
如果GridView有3列,DataSource有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());
}
}
}
if(e.Row.RowType==DataControlRowType.DataRow)
{
//将dataitem强制转换回datarowview
DataRowView drv=e.Row.DataItem作为DataRowView;
//循环datarowview中的所有项(不等于网格中的列)
for(int i=0;i
谢谢您的回复。这适用于sql server表还是datatable?此外,这是否考虑到我不知道将有多少列?它将处理来自任何数据源的数据。您需要知道列的数量,因为gridview具有需要为数据库表中的每列指定的模板字段。谢谢你的回复。这适用于sql server表还是datatable?此外,这是否考虑到我不知道将有多少列?它将处理来自任何数据源的数据。您需要知道列的数量,因为gridview具有需要为数据库表中的每列指定的模板字段。谢谢你的回复。我注意到您正在为单元格设置一个固定值。我不知道datatable将有多少列。您的答案仍然有效吗?这取决于如何从数据源接收列及其颜色。如果是text、color、text、color
等,您可以在rowdatabound事件中创建一个循环,为每个单元格-1
着色,非常感谢更新。通过您的更新,我能够为我的案例更改for
循环。非常好用。谢谢你的回复。我注意到您正在为单元格设置一个固定值。我不知道datatable将有多少列。您的答案仍然有效吗?这取决于如何从数据源接收列及其颜色。如果是text、color、text、color
等,您可以在rowdatabound事件中创建一个循环,为每个单元格-1
着色,非常感谢更新。通过您的更新,我能够为我的案例更改for
循环。而且效果很好。