C# GridView行数据绑定处理程序-Can';无法从行中获取数据
我有一个匿名类型的GridView。我需要检查单元格中的值,如果满足条件,则突出显示该单元格。问题是,当我试图从行的单元格中提取数据时,总是得到一个空字符串。我已成功突出显示了所有单元格,并在Visual Studio 2010调试器中进行了检查,并确认该行具有数据(该行的DataItem具有我需要的值)。这是在回发中发生的,我不确定这是否是个问题 以下是我尝试过的代码和解决方案:C# GridView行数据绑定处理程序-Can';无法从行中获取数据,c#,.net,asp.net,gridview,C#,.net,Asp.net,Gridview,我有一个匿名类型的GridView。我需要检查单元格中的值,如果满足条件,则突出显示该单元格。问题是,当我试图从行的单元格中提取数据时,总是得到一个空字符串。我已成功突出显示了所有单元格,并在Visual Studio 2010调试器中进行了检查,并确认该行具有数据(该行的DataItem具有我需要的值)。这是在回发中发生的,我不确定这是否是个问题 以下是我尝试过的代码和解决方案: protected void grvValidCourses_RowDataBound(Object sender
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>
找不到有效的课程。
更新:我一直在尝试你的所有建议,但我得到了异常、null或空字符串。我甚至在一个更简单的例子中重新创建了这个问题,但仍然无法解决它!不过,我会继续尝试,并感谢您提出的任何新建议。第1部分-缺少的文本
由于需要访问子控件,第一部分中可能会出现空白值:
String str = ((DataBoundLiteralControl)e.Row.Cells[6].Controls[0]).Text;
要查看单元格在调试模式下是否有任何值(请检查“调试输出”窗口中的文本):
第2部分-控制缺失
这是错误的:
Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null
无法在gridview中搜索行的控件。您需要搜索该行
Label lblProductOptionGrpName = (Label)e.Row.FindControl("lblProductOptionGrpName");
第3部分-访问数据项
最后,我不确定您正在使用匿名类型做什么,但您可能需要在访问属性之前检查内容:
if(MyControl.GetType() == typeof(HyperLink))
{
HyperLink TestLink = (HyperLink)MyControl;
TestLink .Visible = false;
}
如果您可以发布标记,那么就有可能解决您的问题 你这样做是错误的 这
DataRowView rowView=(DataRowView)e.Row.DataItem代码>
应该是DataRowView=((DataRowView)e.Row.DataItem).Row代码>好的,我终于弄明白发生了什么!我只能通过行访问绑定字段(例如e.row.Cells[index].Text
)。因此,我使用绑定字段获取项目的ID,然后在数据库中找到该项目,获取日期,进行比较,并突出显示行的单元格。这不是最有效的方法,但它是有效的
来自简单示例的代码:
网格视图前端
<asp:GridView ID="gdv" runat="server" AutoGenerateColumns="True" OnSelectedIndexChanged="gdv_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Select" />
<asp:BoundField DataField="id" HeaderText="#" ReadOnly="True" />
<asp:TemplateField HeaderText="CategoryName" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" >
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("desc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblSelected" runat="server" Text="No row selected"></asp:Label>
这给了我一个语法错误:错误4无法将类型“System.Data.DataRow”隐式转换为“System.Data.DataRowView”仍然获得无效的强制转换异常:无法强制转换类型为“f_uAnonymousType0”的对象5[System.String,System.Nullable….键入'System.Data.DataRowViewI在运行时尝试第一行时遇到异常:无法将'System.Web.UI.LiteralControl'类型的对象强制转换为'System.Web.UI.DataBoundLiteralControl'类型。我尝试将其强制转换为LiteralControl,但得到了“/r/n”对于str,我将尝试您的其余建议。我尝试将调试器输出与您的第1部分建议一起使用,但在输出中没有看到任何有用的内容(当然不是单元格值)。我尝试了第2部分,但标签返回为空(文本为空字符串)。我不确定我是否真正理解您的第3部分。受保护的void grvValidCourses_RowDataBound(对象发送者,GridViewRowEventArgs e){if(e.Row.RowType==DataControlRowType.DataRow){Label lbl=(Label)e.Row.FindControl(“lblEndDate”);String str=lbl.Text;您应该包括解释为什么/如何解决问题,而不仅仅是发布代码。
if(MyControl.GetType() == typeof(HyperLink))
{
HyperLink TestLink = (HyperLink)MyControl;
TestLink .Visible = false;
}
<asp:GridView ID="gdv" runat="server" AutoGenerateColumns="True" OnSelectedIndexChanged="gdv_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Select" />
<asp:BoundField DataField="id" HeaderText="#" ReadOnly="True" />
<asp:TemplateField HeaderText="CategoryName" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" >
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("desc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblSelected" runat="server" Text="No row selected"></asp:Label>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
var qry = ctx.Categories
.Select(c => new {
id = c.CategoryID,
name = c.CategoryName,
desc = c.Description,
});
gdv.DataSource = qry;
gdv.DataBind();
}
}
protected void gdv_SelectedIndexChanged(object sender, EventArgs e) {
selectRow();
}
private void selectRow() {
GridViewRow row = gdv.SelectedRow;
String strId = row.Cells[1].Text; // Bound Field column
lblSelected.Text = strId;
// use ID to get object from database...
}
protected void gridPanne_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
Label label = (Label)e.Row.FindControl("Label4");
lbl.Text += " ** / ** "+label.Text;
}
}