2013-04-11 78 views
0

我想在RowDataBound函数中取LangId的值。这个怎么做?在GridView中获取BoundField的值RowDataBound事件

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

回答

0

当时的数据对象为e.Row.DataItem。你只需要把它作为适当的类型。

var myItem = (MyType)e.Row.DataItem; 
// myItem.LangId now available 
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;   
    } 
} 
0

您可以通过此获得它:

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

ColumnIndexOfYourBoundField意味着如果你列第一列比它的索引是0,如果它的第二个比它的1等等。

3

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

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

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

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