0

I am binding grid view with datatable, I want to compute some values after binding The easy and common way to achieve this is to do all the computation in RowDataBound event, find controls in the row and fill them with the computed values. But currently I don't have any control in my grid view, rather I am showing values using Eval method directly.

I am wondering if there is any way to update data item associated with the grid view rows in DataBound event so that I can avoid adding controls and can directly access those computed columns in template field, something like this

protected void gvSuspensions_DataBound(object sender, EventArgs e)
{
   foreach (GridViewRow row in gvPreviousSuspensions.Rows)
   {
      DataRowView rowView = (DataRowView)row.DataItem;// getting DataItem null :(
      rowView["ComputedColumn"] = "ComputedValue";
   }
}

And can use it directly like this

<asp:TemplateField>
    <ItemTemplate>
       <%# Eval("ComputedColumn") %>
    </ItemTemplate>
</asp:TemplateField>

But I am getting null row.DataItem!

Any idea?

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105

1 Answers1

0

You should be using RowDataBound event instead, which gives you a reference to the current row being bound.

protected void gvSuspensions_RowDataBound(Object sender, GridViewRowEventArgs e)

And e.Row.DataItem is what you should be accessing.

Referece for RowDataBound

Icarus
  • 63,293
  • 14
  • 100
  • 115