获取GridView RowDataBound事件中的BoundField值



我想在RowDataBound函数中取LangId值。如何做到这一点?

<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />
protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // need LangId
        ImageButton imgBtn = (ImageButton)e.Row.FindControl("imgBtnDelete");
        imgBtn.Visible = false;
    }
}

有几种方法可以做到。也许更多。

<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />
protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       string langId = e.Row.Cells[columnIndex].Text; // one of the ways
       string langId2 = DataBinder.Eval(e.Row.DataItem, "LangId").ToString(); // one of the other ways
    }
}

您可以通过以下方式获取:

string str = e.Row.Cells[CloumnIndexOfYourBoundField].Text;

ColumnIndexOfYourBoundField表示如果您的列是第一列,则其索引为0,如果它是第二列,则其索引为1,以此类推。

数据对象为e.Row.DataItem。您只需要将其转换为适当的类型。

var myItem = (MyType)e.Row.DataItem;
// myItem.LangId now available

。ASPX文件:

<ItemTemplate>
    <asp:ImageButton ID="imgEdit" runat="server" AlternateText="Edit"
        CommandArgument='<%# Eval("LangId") %>' CommandName="DeleteLedger" ToolTip="Delete"
        ImageUrl="~/App_Themes/DefaultClient/images/Delete.png" />
</ItemTemplate>

cs文件:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    DataRowView dataRow = ( DataRowView ) e.Item.DataItem;
    string strLangId = dataRow["LangId"].ToString();
    DataTable dtData1 = 
    objAccountTypeBAL.ChkLedgerRelation(Convert.ToInt64(strLangId ), objSession.BranchId);
    if (dtData1.Rows.Count > 0)
    {
        ImageButton img = (ImageButton)item["Delete"].Controls[0];
        img.Visible = false;          
    }
}

使用动态类型,您可以访问记录中的字段:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    dynamic data = e.Row.DataItem;
    int LangId = data.LangId;
    // do your code here
}
 protected void GrdEmplistFromAtt_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("onmouseover", "MouseEvents(this, event)");
            e.Row.Cells[0].Attributes.Add("onmouseout", "MouseEvents(this, event)");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl_GrdCode = (Label)e.Row.FindControl("lblGrdCode");
}
}

相关内容

  • 没有找到相关文章

最新更新