分别填充和绘制每个gridview单元格? [英] Fill and paint each gridview cell individually?
问题描述
ColNum Color RowNum消息
1 Gold 1 Text1
1 Black 2 Text2
2 Red 2 MoreText
2 Blue 3 TextX
我的存储过程像这样返回相同的数据。这是底层的 datatable
:
Col1 Col2
$ b
- ---------------------
黄金(空白)
黑色红色
(空白)蓝色
我填写
RowDataBound
中的每个单元格:protected void GridViewClicks_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
foreach(e.Row.Cells中的TableCell单元格)
{
cell.BackColor = ConvertFromHexToColor(cell.Text);
这段代码的工作原理是因为它填满了单元格与其相应的背景颜色。
现在的问题是我还需要在每个单元格中显示我的sql表[dbo.Clicks]的内容。这就是我卡住的地方。另一种方法是,如果我使用示例数据,每个数据表单元都包含颜色和文本,就像这样。然后我解析它:
Col1 Col2
Gold / Text1(null)
Black / Text2红色/ MoreText
(null)Blue / TextX
但我认为必须有一个更优雅的方式来做到这一点。对我而言,这个解决方案非常难看。
我的gridview看起来像这样:
< asp:GridView ID =GridViewClicksrunat =serverShowHeader =Falseonrowdatabound =GridViewClicks_RowDataBound>
< / asp:GridView>
谢谢。
解决方案您可以在
OnRowDataBound
事件中使用DataRowView
,并从一行中获取单个值,将它们应用到特定的单元格。if(e.RowType == DataControlRowType.DataRow)
{
//将dataitem转换回datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//使用数据视图中的数据为特定单元格指定颜色和内容
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 .RowType == DataControlRowType.DataRow)
{
//将数据项转换回datarowview
DataRowView drv = e.Row.DataItem as DataRowView;
//循环显示datarowview中的所有项目(不等于grid中的列)
for(int i = 0; i< drv.Row.ItemArray.Length; i ++)
{
//检查它是否是不平衡的列
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());
}
}
}
I have sql table dbo.Clicks that looks like this:
ColNum Color RowNum Message 1 Gold 1 Text1 1 Black 2 Text2 2 Red 2 MoreText 2 Blue 3 TextX
My stored procedure returns this same data like this. This is underlying
datatable
:Col1 Col2 ----------------------- Gold (null) Black Red (null) Blue
I fill each cell in
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); } } }
This code works because it fills the cells with its corresponding backcolor.
The issue now is that I also need to display the contents of my sql table [dbo.Clicks] in each cell. And that's where I'm stuck.
An alternative is that each datatable cell includes the color and the text, something like this, if I use the sample data. Then I parse it:
Col1 Col2 Gold/Text1 (null) Black/Text2 Red/MoreText (null) Blue/TextX
But I'm thinking there has to be a more elegant way of doing this. For me, this solution's pretty ugly.
My gridview looks like this:
<asp:GridView ID="GridViewClicks" runat="server" ShowHeader="False" onrowdatabound="GridViewClicks_RowDataBound"> </asp:GridView>
Thanks.
解决方案You can use the
DataRowView
in theOnRowDataBound
event and get the individual values from a row and apply them to specific cells.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
If the GridView has 3 columns, and the DataSource 6, with alternating text/color values you can create a loop
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()); } } }
这篇关于分别填充和绘制每个gridview单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!