我正在使用sqldatasource和GridView.我想在RowDataBound事件中从GridView获取单元格的值?因为我无法使用e.RowIndex.
如果单元格为空,如何检查updatng事件?我用过if != null,但它没有用,所以我需要检查它是否为空.
谢谢
在gridview’s RowDataBound事件有e.Row.RowIndex和e.Row.DataItemIndex属性.
请告诉我容易理解的答案,它们之间有什么区别?
在哪种情况下我们应该使用哪一个?
我有一个ASP.NET Web窗体应用程序.在我的应用程序中,我有一个可以顺利运行的GridView.我有几个文本字段,最后一个是<asp:hyperlinkfield>.
现在我想通过放置一个简单的链接而不是在hyperlinkfield满足特定条件的情况下以编程方式更改字段.所以我抓住了这个onRowDataBound事件:
Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles myGridView.RowDataBound
If (condition) Then
Dim link = New HyperLink()
link.Text = "login"
link.NavigateUrl = "login.aspx"
e.Row.Cells(3).Controls.Add(link)
End If
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
其中nhyperlinkfield是放置的单元格.使用此代码,它只是增加hyperlinkfield了新的代码link.我怎样才能更换它?
PS:代码在VB6中,但我是C#程序员,接受两种语言的答案
我在aspx页面中有一个gridview:
<asp:GridView ID="gdvMainList" runat="server" CssClass="Grid1" SkinID="PagedGridView"
AutoGenerateColumns="false" OnRowDataBound="gdvMainList_RowDataBound"
DataSourceId="dtsConsumers" Visible="false" DataKeyNames="Id">
<Columns>
<asp:CommandField SelectText="Select" ShowSelectButton="true" ItemStyle-CssClass="HideButton"
HeaderStyle-CssClass="HideButton">
<HeaderStyle CssClass="HideButton" />
<ItemStyle CssClass="HideButton" />
</asp:CommandField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<span>
<%# Pc.PrecisionCare2.PL.Common.Utility.GetFullName("", Eval("LastName"), Eval("FirstName"), Eval("MiddleInit")) %></span>
</ItemTemplate>
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status"></asp:BoundField>
</Columns>
<SelectedRowStyle CssClass="SelectedItem" BackColor="#c9e0ee" />
<EmptyDataTemplate>
<div class="divEmptyGrid">
--- No Consumer Exists ---
</div>
</EmptyDataTemplate>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
该rowDataBound方法是:
protected void gdvMainList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gdvMainList, "Select$" + e.Row.RowIndex); …Run Code Online (Sandbox Code Playgroud) 当值为1时,我想将gridview列的值更改为active.我有gridview列之类的
<asp:BoundField DataField="STATUS" HeaderText="STATUS" SortExpression="STATUS" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
Run Code Online (Sandbox Code Playgroud)
和cs代码
protected void gvCategory_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[5].Text=="0")
{
e.Row.Cells[5].Text = "INACTIVE";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,但如果我更改列顺序,它将失败.我需要的是像findControl函数.谢谢.
我有一个GridView:
<asp:GridView ID="gvDownloads">
<Columns>
<asp:TemplateField HeaderText="Status" >
<ItemTemplate>
<%# Eval("Enabled")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<asp:GridView/>
Run Code Online (Sandbox Code Playgroud)
该Enabled属性是一个布尔值.现在我想根据Enabled属性的True/False显示Enabled/Disabled .因此我使用:
Sub gvDownloads_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvDownloads.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(3).Text = "True" Then
e.Row.Cells(3).Text = "Enabled"
Else
e.Row.Cells(3).Text = "Disabled"
End If
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为启动事件时 e.Row.Cells(3).Text是一个空字符串.我怎么解决这个问题?谢谢
我想根据登录的相关用户启用或禁用特定行.所以我使用rowdatabound事件,但我在这一行有一个错误:
DataRow drv = ((DataRowView)e.Row.DataItem).Row;
Run Code Online (Sandbox Code Playgroud)
在这里,e.Row.DataItem具有相关的行信息.我控制了它,它有行的值.但是当我想继续时,我会接受这个错误:
无法将类型为"<> f__AnonymousType0
14[System.Int32,System.Int32,System.String,System.String,System.DateTime,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable1 [System.DateTime],System.String,System.Nullable .1 [System.Boolean]]'的对象转换为"System.Data.DataRowView".
然后我改变了这一行:
DataRowView drv = e.Row.DataItem as DataRowView;
Run Code Online (Sandbox Code Playgroud)
对于这种情况,它没有给出错误,但是drv仍然是空值.Dataitem不会分配其值.
在这里,相关的完整代码:
protected void gvListele_RowDataBound(object sender, GridViewRowEventArgs e)
{
dbeDataContext db = new dbeDataContext();
var c = (from v in db.CAGRIs where v.UserID != Convert.ToInt32(Session["user"]) select v).ToArray();
if (c != null)
{
foreach (var item in c)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow drv = ((DataRowView)e.Row.DataItem).Row;
int tempID = Convert.ToInt32(drv["CagriID"].ToString());
if (item.CagriID == tempID)
{
e.Row.Enabled = false;
} …Run Code Online (Sandbox Code Playgroud) 我有Grd_RowDataBound,我正在将颜色应用到我的GridView Rows.我使用下面的代码在Crome和Mozilla中工作正常,但在IE11中不起作用.在IE11中,我的Gridview行返回颜色不起作用.
protected void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string Status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Visit_Status"));
if (Status == "1")
{
e.Row.BackColor = ColorTranslator.FromHtml("#28b779");//81F79F
}
else
{
e.Row.BackColor = ColorTranslator.FromHtml("#da5554");//F78181
}
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙.
目前,我在RowDataBound中有以下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label groupID = (Label)e.Row.FindControl("idgroup");
LinkButton myLink = (LinkButton)e.Row.FindControl("groupLink");
myLink.Attributes.Add("rel", groupID.Text);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我单击"编辑"链接时,它会尝试运行该代码并引发错误.因此,如何在GridView处于读取模式时运行该代码?但不是在编辑时......
我想在某些条件下在GridView单元格上添加一个按钮.我在RowDatabound事件中执行了以下操作
if( i==0)
{
Button btn= new Button();
btn.Text = "view more";
e.Row.Cells[7].Controls.Add(btn);
}
Run Code Online (Sandbox Code Playgroud)
执行此操作时,绑定的单元格中的文本将丢失,并且仅显示按钮.我需要有按钮以及已经存在的单元格文本.
有人可以帮我这样做吗?提前致谢
我有一个 gridview,它检查rowDataBound事件上的一些值。我想根据 rowDataBound 中检查的条件删除一些行。我尝试将所有控件放在面板中并隐藏该面板,即,
尝试1:
protected void grdFeatured_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//some other codes here
//IMPLEMENT FILTER ACCORDING TO ABOVE 'VIS' OUTPUT
if (vis > 0)
{
Panel1.Visible = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题 :
这会扰乱分页,因为行被隐藏,但页计数发生并显示剩余可见行的页码。
尝试2:
protected void grdFeatured_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//some other codes here
//IMPLEMENT FILTER ACCORDING TO ABOVE 'VIS' OUTPUT
if (vis > 0)
{
gvr.Parent.Controls.RemoveAt(gvr.RowIndex);
}
} …Run Code Online (Sandbox Code Playgroud) 我正在转换一些类C#,VB.NET并且此行无法正确转换.我尝试了很少的在线转换器,但它们似乎没有用.
this.mGrid.RowDataBound += new GridViewRowEventHandler(RowDataBoundHandler);
Run Code Online (Sandbox Code Playgroud)
转换后VB.NET不起作用.
Me.mGrid.RowDataBound += New GridViewRowEventHandler(RowDataBoundHandler)
Run Code Online (Sandbox Code Playgroud)
以下是我从中得到的两个错误.任何人都可以帮助我,因为我的大脑已经死了.
谢谢,
Error 4 Delegate 'System.Web.UI.WebControls.GridViewRowEventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor. C:\My Projects\PMS\App_Code\GridViewHelper.vb 110 62 C:\My Projects\PMS\
Error 3 'Public Event RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. C:\My Projects\PMS\App_Code\GridViewHelper.vb 110 9 C:\My Projects\PMS\