在GridView RowDataBound事件中获取BoundField值

时间:2013-04-11 07:14:04

标签: c# asp.net gridview 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;
    }
}

6 个答案:

答案 0 :(得分:4)

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

<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
    }
}

答案 1 :(得分:1)

你可以通过这个得到它:

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

ColumnIndexOfYourBoundField表示你的列是第一列而不是索引是0,第二列是否为1,依此类推。

答案 2 :(得分:0)

此时数据对象以e.Row.DataItem的形式提供。你只需要把它作为合适的类型。

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

答案 3 :(得分:0)

.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;          
    }
}

答案 4 :(得分:0)

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

if (e.Row.RowType == DataControlRowType.DataRow)
{
    dynamic data = e.Row.DataItem;
    int LangId = data.LangId;
    // do your code here
}

答案 5 :(得分:0)

 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");
}
}