gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:01:02 25 4
gpt4 key购买 nike

(例如根据隐藏值设置行颜色)

如果你有一个隐藏单元格的 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;

}

作为一个相当绿色的人,我花了很多时间在许多论坛上搜索和调试,以找出处理程序看不到隐藏的数据绑定(bind)字段,我似乎找不到如何根据隐藏字段设置 rowcolor 的答案,所以我虽然 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"]);

关于asp.net - c#使用onrowdatabound从gridview asp.net中的隐藏单元格获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com