【问题标题】:Fill and paint each gridview cell individually?分别填充和绘制每个gridview单元格?
【发布时间】:2017-04-08 05:44:58
【问题描述】:

我有如下所示的 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

但我认为必须有一种更优雅的方式来做到这一点。对我来说,这个解决方案非常难看。

我的网格视图如下所示:

<asp:GridView ID="GridViewClicks" runat="server" ShowHeader="False" onrowdatabound="GridViewClicks_RowDataBound">
</asp:GridView>

谢谢。

【问题讨论】:

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


    【解决方案1】:

    您可以在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());
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。我注意到您正在为Cells 设置一个固定值。我不知道数据表会有多少列。你的答案还能用吗?
    • 这取决于如何从数据源接收列及其颜色。如果是 text,color,text,color 等,您可以在 rowdatabound 事件中创建一个循环,为每个 cell -1 着色
    • 非常感谢您的更新。通过您的更新,我能够为我的案例更改 for 循环。而且效果很好。
    【解决方案2】:

    您可以使用带有标签的模板字段并使用 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")) %>
    

    【讨论】:

    • 感谢您的回复。这适用于 sql server 表还是数据表?另外,这是否考虑到我不知道会有多少列?
    • 它适用于来自任何数据源的数据。您需要知道列数,因为 gridview 具有您需要为数据库表中的每一列指定的模板字段。 .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多