5

我有一个大的网格视图:

<asp:GridView CssClass="hoursGrid" ID="hoursReportGridView" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84"
    BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource2" OnRowDataBound="hoursReportGridView_OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Person" HeaderText="Person" SortExpression="Project" />
        <asp:BoundField DataField="Project" HeaderText="Project" SortExpression="Project" />
        <asp:BoundField DataField="ProjectType" HeaderText="Project Type" ReadOnly="True" SortExpression="Sprint" ItemStyle-HorizontalAlign="Center" />
        <asp:BoundField DataField="StoryNumber" HeaderText="Story Number" SortExpression="Story" ItemStyle-Width="6%" ItemStyle-HorizontalAlign="Center" />
        <asp:BoundField DataField="StoryTitle" HeaderText="Story Title" SortExpression="Story" ItemStyle-Width="20%" />
        <asp:BoundField DataField="Effort" HeaderText="Effort" SortExpression="Effort" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task"  ItemStyle-Width="20%" />
        <asp:BoundField DataField="OriginalEstimateHours" HeaderText="Original Estimate" SortExpression="OriginalEstimateHours" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Monday" HeaderText="Monday" ReadOnly="True" SortExpression="Monday" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Tuesday" HeaderText="Tuesday" ReadOnly="True" SortExpression="Tuesday" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Wednesday" HeaderText="Wednesday" ReadOnly="True" SortExpression="Wednesday" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Thursday" HeaderText="Thursday" ReadOnly="True" SortExpression="Thursday" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Friday" HeaderText="Friday" ReadOnly="True" SortExpression="Friday" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Saturday" HeaderText="Saturday" ReadOnly="True" SortExpression="Saturday" ItemStyle-HorizontalAlign="Right" />
        <asp:BoundField DataField="Sunday" HeaderText="Sunday" ReadOnly="True" SortExpression="Sunday" ItemStyle-HorizontalAlign="Right" />
        <asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Right">
            <ItemTemplate>
                <asp:LinkButton ID="taskLinkButton" Text='<%# Eval("Total") %>' Visible='<%# Eval("StoryTitle").ToString() != "" %>' runat="server" OnClick="taskLinkButton_Click" />
                <asp:Literal ID="Literal1" Text='<%# Eval("Total") %>' Visible='<%# Eval("StoryTitle") == "" %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
         <asp:BoundField DataField="DifferentUsers" HeaderText="DifferentUsers" SortExpression="DifferentUsers" Visible="false"/>
    </Columns>
</asp:GridView>

我不想向用户显示的最后一个边界域,这就是它的可见性为假的原因。

但是,如果该行的此不可见单元格> 0,我想更改单元格的颜色:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.Cells[16].Text != "&nbsp;") && (Int16.Parse(e.Row.Cells[16].Text) > 0))
        {
            for (int i = 0; i < 15; i++)
            {
                e.Row.Cells[i].ForeColor = Color.Black;
                e.Row.Cells[i].BackColor = ColorTranslator.FromHtml("#fde16d");
            }
        }
    }
}

此方法适用于列可见,但当我将其设置为 false 时它不起作用。如何在不显示列的情况下实现功能?

4

6 回答 6

10

不要隐藏该单元格,而是使用TemplateField包含 ASP.NETHiddenField控件的 a,如下所示:

<asp:TemplateField>
    <ItemTemplate>
        <asp:HiddenField ID="HiddenFieldDifferentUsers" Value='<%# Eval("DifferentUsers") %>' runat="server" />
    </ItemTemplate>
</asp:TemplateField>

现在在您的代码隐藏中,您可以找到隐藏字段控件,如下所示:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HiddenField theHiddenField = e.Row.FindControl("HiddenFieldDifferentUsers") as HiddenField;

        // Check that we successfully found hidden field before using it
        if(theHiddenField != null)
        {
            // Do something with hidden field here if you need to
        }
    }
}
于 2013-11-05T15:57:20.740 回答
3

尝试这个

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
       DataRowView rowView = (DataRowView)e.Row.DataItem;

       int a = Convert.ToInt32(rowView["DifferentUsers"]);

       if(a>0)
        {

            for (int i = 0; i < 15; i++)
            {
                e.Row.Cells[i].ForeColor = Color.Black;
                e.Row.Cells[i].BackColor = ColorTranslator.FromHtml("#fde16d");
            }
        }

}

参考

于 2013-11-05T16:02:46.217 回答
1

您可以尝试在更改颜色之前将可见性设置为 true,然后再设置为 false。用户将永远不会看到该列,因为在操作期间网格不会被刷新。

于 2013-11-05T15:54:41.590 回答
0

根据您绑定到 的方式GridView,可以将行DataItem转换为您的数据类:

protected void hoursReportGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    MyData myData = e.Row.DataItem as myData;
    if (myData != null && myData.DifferentUsers > 0)
    {
        e.Row.ForeColor = Color.Black;
        e.Row.BackColor = ColorTranslator.FromHtml("#fde16d");
    }
}

这在开发时提供 IntelliSense,并在编译时提供强类型检查。

于 2014-10-29T15:14:38.030 回答
0

试试这个朋友,

我取了一个带有列 BranchID 和 Branchname 的表 Branch 来隐藏 BranchID 我在 TemplateField 中取了 HiddenField,如下所示

<asp:TemplateField>
    <ItemTemplate>
      <asp:HiddenField ID="BranchID" Value='<%# Eval("BranchID") %>' runat="server" />
    </ItemTemplate>
</asp:TemplateField>

在 aspx.cs 中

 HiddenField hd = row.FindControl("BranchID") as HiddenField;                 
string str = "UPDATE Branch set BranchName='" + obj.BranchName + "' where BranchId = " + hd.Value + " ";
于 2014-11-05T06:33:30.220 回答
0

使用 VB 而不是 C#,我只能使用它:

Dim myWordList As String = e.Row.DataItem("myWordList").ToString

即使用e.Row。DataItem ("DataFieldName") 而不是 e.Row。FindControl ("DataFieldName")。

要检索隐藏绑定字段的值:

<asp:BoundField DataField="myWordList" HeaderText="myWordList" 
                        SortExpression="BadWordList" Visible="False" />

我相信这是等效的 C#:

string myWordList = e.Row.DataItem("myWordList").ToString; 

这个对我有用。;) 事实上,看起来绑定列甚至不需要在 gridview 中,只要数据字段在返回的记录集中。

于 2021-09-14T09:20:15.460 回答