我有一个动态创建的gridview,现在我试图通过保持链接按钮来编辑每一列。但是rowdatabound事件没有被触发。我可能出错了什么? 以下是我的代码:
dtValues = gObj.GetAllDocumentsHistoryList();
dtHeader = gObj.GetAllHeaderList();
GridView gvEmployee = new GridView();
gvEmployee.ShowHeaderWhenEmpty = true;
gvEmployee.EmptyDataText = "Sorry No History Records Found !!!!!!!";
gvEmployee.AutoGenerateColumns = false;
for (int i = 0; i < dtValues.Columns.Count; i++)
{
string name = dtValues.Columns[i].ColumnName.ToString();
BoundField boundfield = new BoundField();
boundfield.DataField = dtValues.Columns[i].ColumnName.ToString();
for (int j = 0; j < dtHeader.Rows.Count; j++)
{
if (dtHeader.Rows[j]["ColCode"].ToString() == dtValues.Columns[i].ColumnName.ToString())
{
boundfield.HeaderText = dtHeader.Rows[j]["ColName"].ToString();
if (boundfield.HeaderText.Contains("Date") || boundfield.HeaderText.Contains("DocExpiry"))
{
boundfield.DataFormatString = "{0:dd/MMM/yyyy}";
}
}
else if (dtValues.Columns[i].ColumnName.ToString() == "Last Modified Date")
{
boundfield.HeaderText = "Last Modified Date";
boundfield.DataFormatString = "{0:dd/MMM/yyyy}";
}
}
gvEmployee.Columns.Add(boundfield);
}
gvEmployee.DataSource = dtValues;
gvEmployee.DataBind();
gvEmployee.Width = new Unit("90%");
gvEmployee.RowDataBound += new GridViewRowEventHandler(gvEmployee_RowDataBound);
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
e.Row.Cells[3].Controls.Add(lnkView);
}
答案 0 :(得分:0)
只需在DataBind之前添加RowDataBound处理程序。
更正后的代码:
gvEmployee.DataSource = dtValues;
gvEmployee.RowDataBound += new GridViewRowEventHandler(gvEmployee_RowDataBound);
gvEmployee.DataBind();
gvEmployee.Width = new Unit("90%");