Gridview RowDataBound事件后面的代码

时间:2010-02-24 21:19:47

标签: asp.net gridview

我已经在代码后面创建了一个gridview(就像它实际上没有出现在页面上一样)。我想知道如何调用它的 rowdatabound 事件 - 因为数据被绑定到它。碰巧有一个Gv.RowDataBound函数,但我该如何使用它?

(我想要与asp:gridview控件的onrowdatabind属性相同的功能......)


GridView Gv = new GridView();
Gv.AutoGenerateColumns = false;

BoundField one = new BoundField();
one.DataField = "one";
one.HeaderText = "One";
Gv.Columns.Add(one);

BoundField two = new BoundField();
one.DataField = "two";
one.HeaderText = "Two";
Gv.Columns.Add(two);


//dt is a datatable with some data

Gv.DataSource = (dt);
Gv.DataBind();

2 个答案:

答案 0 :(得分:8)

使用以下方法设置gridview的事件处理程序:

 Gv.RowDataBound += new GridViewRowEventHandler(Gv_RowDataBound);

然后创建自己的事件处理程序

 void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Do whatever you want in here.
    }

答案 1 :(得分:1)

要在VB中实现此目的,请使用:

AddHandler Gv.RowDataBound, AddressOf Gv_RowDataBound