从隐藏列获取单元格值

时间:2013-11-05 15:51:01

标签: c# asp.net gridview

我有一个很大的gridview:

<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>

我不想向用户显示的最后一个边界字段,这就是为什么它的可见性是错误的。

但是,如果该行的不可见单元格> 1,我想更改单元格的颜色。 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时它不起作用。如何在不显示列的情况下实现功能?

5 个答案:

答案 0 :(得分:10)

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

<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
        }
    }
}

答案 1 :(得分: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");
            }
        }

}

reference

答案 2 :(得分:1)

您可以尝试查看在更改颜色之前将可见性设置为true,并在此之后将其设置回false。用户永远不会看到该列,因为在操作过程中网格不会刷新。

答案 3 :(得分: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,在编译时提供强类型检查。

答案 4 :(得分:0)

试试这个朋友,

我拿了一个表格分支Colums BranchID&amp; Branchname隐藏BranchID我把HiddenField放在里面 TemplateField如下所示

<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 + " ";