加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – GridView RowDataBound处理程序 – 无法从行获取数据

发布时间:2020-12-15 19:56:10 所属栏目:百科 来源:网络整理
导读:我有一个匿名类型的GridView.我需要检查单元格中的值,并在满足条件时突出显示该单元格.问题是,当我尝试从行的单元格中提取数据时,我总是得到一个空字符串.我已成功突出显示所有单元格,并且已在Visual Studio 2010调试器中检查并确认Row具有数据(行的DataItem
我有一个匿名类型的GridView.我需要检查单元格中的值,并在满足条件时突出显示该单元格.问题是,当我尝试从行的单元格中提取数据时,我总是得到一个空字符串.我已成功突出显示所有单元格,并且已在Visual Studio 2010调试器中检查并确认Row具有数据(行的DataItem具有我需要的值).这是在PostBack上发生的,我不确定这是不是问题.

这是我尝试过的代码和解决方案:

protected void grvValidCourses_RowDataBound(Object sender,GridViewRowEventArgs e) {
 if (e.Row.RowType == DataControlRowType.DataRow) {
  String str = e.Row.Cells[6].Text.ToString(); // empty string
  Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null
  DataRowView rowView = (DataRowView)e.Row.DataItem; // exception about casting anonymous type

这里发生了什么?为什么我不能从细胞中获取数据?

GridView的标记:

<asp:GridView ID="grvValidCourses" runat="server" Width="790px" OnRowCancelingEdit="grvValidCourses_RowCancelingEdit"
                    OnRowEditing="grvValidCourses_RowEditing" OnRowUpdating="grvValidCourses_RowUpdating" OnRowDeleting="grvValidCourses_RowDeleting"
                    AutoGenerateColumns="False" OnSelectedIndexChanged="grvValidCourses_SelectedIndexChanged"
                    OnRowDataBound="grvValidCourses_RowDataBound" >
                    <Columns>
                        <asp:CommandField ShowEditButton="True" EditText="Edit" UpdateText="Update |" />
                        <asp:TemplateField ShowHeader="False">
                             <ItemTemplate>
                               <asp:LinkButton ID="lbnDelete" runat="server" CausesValidation="False" CommandName="Delete"
                                    Text='<%# (Eval("active") == null ? "Delete" : ((Eval("active").ToString() == "0" ? "Restore" : "Delete"))) %>' />  
                             </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowSelectButton="True" SelectText="Details" />
                        <asp:TemplateField HeaderText="Training Name" SortExpression="coursename">
                            <EditItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("coursename") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("coursename") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="ttNo" HeaderText="#" SortExpression="ttNo" ReadOnly="True" />
                        <asp:TemplateField HeaderText="Course Date" SortExpression="startDate">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("startdate","{0:M/d/yyyy}") %>'></asp:TextBox>
                                <asp:CalendarExtender ID="TextBox3_CalendarExtender" runat="server" Enabled="True"
                                    TargetControlID="TextBox3" Format="M/d/yyyy">
                                </asp:CalendarExtender>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("startdate","{0:M/d/yyyy}") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Expiry Date" SortExpression="endDate">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("enddate","{0:M/d/yyy}") %>'></asp:TextBox>
                                <asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True"
                                    TargetControlID="TextBox1" Format="M/d/yyyy">
                                </asp:CalendarExtender>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblEndDate" runat="server" Text='<%# Bind("enddate","{0:M/d/yyyy}") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>No valid courses found.</EmptyDataTemplate>
                </asp:GridView>

更新:我一直在尝试你的所有建议,但我得到异常或空或空字符串.我甚至在一个更简单的例子中重新创建了这个问题,但仍然无法弄明白!我会继续尝试,我感谢任何新的建议.

解决方法

第1部分 – 缺少文本

由于需要访问子控件,您可能在第一部分中获得空白值:

String str = ((DataBoundLiteralControl)e.Row.Cells[6].Controls[0]).Text;

要查看您的单元格是否在调试模式下具有任何值(在调试输出窗口中检查文本):

void grvValidCourses_RowDataBound(object sender,GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    System.Diagnostics.Trace.WriteLine(e.Row.Cells.Count);
     foreach (TableCell c in e.Row.Cells)
     {
      System.Diagnostics.Trace.WriteLine(c.Text);
     }
  }
}

第2部分 – 缺少控制

这是错的:

Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null

您无法在gridview中搜索行的控件.你需要搜索行.

Label lblProductOptionGrpName = (Label)e.Row.FindControl("lblProductOptionGrpName");

第3部分 – 访问DataItem

DataBinder.Eval(e.Row.DataItem,"ColumnName")

最后,我不确定您使用匿名类型做了什么,但在访问属性之前可能需要检查内容:

if(MyControl.GetType() == typeof(HyperLink))
{
  HyperLink TestLink = (HyperLink)MyControl;
  TestLink .Visible = false;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读