单独填充并绘制每个gridview单元格?

时间:2017-04-08 05:44:58

标签: c# asp.net visual-studio-2010 gridview

我有sql表dbo.Clicks,如下所示:

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>

感谢。

2 个答案:

答案 0 :(得分:2)

您可以使用DataRowView事件中的OnRowDataBound并从一行中获取各个值并将其应用于特定单元格。

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());
        }
    }
}

答案 1 :(得分:1)

您可以将模板字段与标签一起使用,并使用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();

使用模板字段创建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>

这个位可以帮到你

style= <%# String.Concat("background-color:",Eval("Color2")) %>

This is how your gridview will look like