使用onrowdatabound从gridview asp.net c#中的隐藏单元格中获取值

前端之家收集整理的这篇文章主要介绍了使用onrowdatabound从gridview asp.net c#中的隐藏单元格中获取值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
(例如,根据隐藏值设置rowcolor)

如果你有一个像这样隐藏单元格的gridview

<asp:GridView ID="Timeevents" runat="server" 
        OnRowDataBound="Timeevents_RowDataBound"
        OnRowCommand = "Timeevents_RowCommand"
        AutoGenerateColumns="False"> 
        <columns>
        <asp:BoundField DataField="CaseID" HeaderText="CaseID" Visible = "False" />
        <asp:BoundField DataField="caseworkerID" HeaderText="CwID" Visible = "False" />
        <asp:BoundField DataField="EventTypeID" HeaderText="EvTypeID" Visible = "False" />
        <asp:BoundField DataField="CaseWorker" HeaderText="Case Worker" />
        <asp:BoundField DataField="EventDate" HeaderText="Event Date" />
        <asp:BoundField DataField="Code" HeaderText="Code" />
        <asp:BoundField DataField="TotalUnits" HeaderText="Total Units" />
        <asp:BoundField DataField="EventType" HeaderText="Event Type" />
        <asp:BoundField DataField="UnitCost" HeaderText="Unit Cost" />
        <asp:BoundField DataField="TotalCost" HeaderText="Total Cost"/>
        <asp:TemplateField HeaderText="ADD">
                <ItemTemplate> 
                    <asp:Button  ID="AddUnit" runat="server" Text=" +1 " 
                    CommandName="AddUnit" 
                    CommandArgument='<%# Eval("CaseID")+ ";" + Eval("CaseworkerID")+ ";" + Eval("EventDate")+ ";" + Eval("EventTypeID")+ ";" + ("1")%>'/>
                </ItemTemplate> 
            </asp:TemplateField> 
        </Columns>
</asp:GridView>

然后似乎不可能使用(e.Row.Cells [2] .Text)在onRowDatabound处理程序中获取这些值

我通过不将任何BoundFields设置为Visible =“False”解决了这个问题
所以它们是可见的=默认情况下为“true”.在后面的代码获取onRowDatabound处理程序中所需的值,然后使它们不可见.像这样.

protected void Timeevents_RowDataBound(object sender,GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {  // just for the datarows
                int a = (int.Parse(e.Row.Cells[2].Text));

                if (a % 2 == 0)
                {
                    e.Row.BackColor = System.Drawing.Color.Gainsboro;
                }
                else
                {
                    e.Row.BackColor = System.Drawing.Color.White;
                }
            }
            // end if so this applies to header and data rows
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;

        }

是一个相当绿色,它花了我很多的谷歌搜索围绕许多论坛和调试,以确定处理程序无法看到隐藏的数据绑定字段,我似乎无法找到如何基于隐藏字段设置rowcolour的答案所以我虽然id发布此内容供其他人查找

如果任何专家知道更好或替代的方式,也许他们也可以添加一些代码/评论
干杯!

解决方法

我认为你可以使用DataItem,因为它说 here
// the underlying data item is a DataRowView object. 
  DataRowView rowView = (DataRowView)e.Row.DataItem;

  // Retrieve the EventTypeID value for the current row. 
  int a = Convert.ToInt32(rowView["EventTypeID"]);

猜你在找的Android相关文章