0

My GridView using following data binding syntax to bind data. I hope do data conversion before it is really bound to GridView. For example, if the VendorID int property (I use a List as data source) is 0, I wish showing empty string on that field. What kind of event I could exploit? And if you could suggest any code sample?

Thanks a lot.

        <asp:TemplateField HeaderText="Vendor ID">
            <ItemStyle Width="1%" BorderColor="#efefef" BorderWidth="1px" />
            <ItemTemplate>
                <asp:Label runat="server" ID="lblNo" Text='<%# Bind("VendorID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle BorderColor="#efefef" />
        </asp:TemplateField>

2 Answers 2

1

Gridview's RowDataBound Event can be use for this.

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // do wat ever u like to do here. 
      // formating, checking, changing data, etc....

      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

    }

  }
1

hi you can use following code in RowDataboundEvent

  if (DataBinder.Eval(args.Row.DataItem, "VendorID").ToString() == "0")
    {
        ((Label)args.Row.FindControl("lblNo")).Text = "";
    }
    else
    {
        ((Label)args.Row.FindControl("lblNo")).Text = DataBinder.Eval(args.Row.DataItem, "VendorID").ToString();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.