C# .NET - Datalist: Databinder.Eval and Container.Item

Asked By Frank A
13-Feb-07 02:53 PM

I'm using a datalist that displays a business name from a database in a simple manner

<%# DataBinder.Eval(Container.DataItem, "BusinessName", "{0}" %>

How can I format this 'BusinessName' Before displaying it - specifically, if the 'Businessname' in the Container.DataItem Row is NULL, I want to display text from another source. Can I pass this DataItem to a method (in the code behind), process it, and then pass it back for display? (I want to programmatically intercept the DataItem, change its value if needed...have thought about doing this before binding, using the Dataset instead, but may not be the easiest way)

Thanks

 

Two options...  Two options...

13-Feb-07 03:17 PM
The first is to use the DataFormatString property in the column itself. In addition, you can use the NullDisplayText field to set a value to be displayed if the data value is null. However, this will only give you limited options on formatting, especially if you're looking at an empty string, which is different than a null value.

The second option, and the one I'd suggest, would be to move your text formatting to the codebehind page and consume the ItemDataBound event. By putting things in the code behind, you have a lot more flexibility in determining what will be displayed to the user, plus you don't have to go into source view on the aspx page when you're trying to debug formatting issues.

Here's a sample of how a method would work.
private void FormatGrid(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
DataRow drData;

// The layout of the grid cells is as follows: // 0 - ID // 1 - Level // 2 - Timestamp // 3 - Source // 4 - Message // 5 - Exception // Only work with data rows if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get corresponding datarow for row we are binding.
drData = ((DataRow)(((System.Data.DataRowView)(e.Row.DataItem)).Row));

if (!(drData.IsDBNull(drData.Item(2)))
{
e.Row.Cells(2).Text = drData.Item(2).ToString();
}
else
{
e.Row.Cells(2).Text = "Not Available.";
}
}
}

You can also pass it into a method for this-  You can also pass it into a method for this-

13-Feb-07 03:30 PM

<%#  FormatMyName(Eval( "BusinessName") )%>

 

// in your codebehind:

protected string FormatMyName( object item)

{

// your "N/A" logic here, and return the final string value to display

}

Thanks Sean and Peter  Thanks Sean and Peter

13-Feb-07 04:08 PM

Went with Peters suggestion and it worked - only I had to fill in the details of the Databinder string

i.e.

<%# evaluate(DataBinder.Eval(Container.DataItem,"VacancyName")) %>

and then:

protected string evaluate (object check)

{    if (check = = system.dbnull.value)

         {...

Sean, I'm using a datalist and still using ASP.Net 1.1 (failed to mention)

 

Thanks!

Create New Account
help
xx' ex: 99999999 0 / p: 999-999-99 protected void OnRowDataBound(object sender, EventArgs e) { GridViewRowEventArgs ea = e as GridViewRowEventArgs; if (ea.Row.RowType = = DataControlRowType.DataRow) { DataRowView drv = ea.Row.DataItem as DataRowView; Object ob = drv["Phone"]; if (!Convert.IsDBNull(ob)) { Int64 iParsedValue = 0; if (Int64.TryParse(ob
c} on columns that are autogenerated using coding? Thanks, Victor ASP.NET Web Controls Discussions DataControlRowType (1) GridViewRowEventArgs (1) DataRowView (1) GridView (1) DataRow (1) RowDataBound (1) RowCreated (1) Format (1) Hi Victor, From your customization against the bound data in code behind. e.g. = = = = = = = = = = = = = = = = = = = protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.DataRow) { DataRowView drv = (DataRowView)e.Row.DataItem; e.Row.Cells[0].Text = string.Format("id_{0}", e.Row.Cells make any changes there. Something along the lines of (psuedo-code): If (e.Row.RowType = DataControlRowType.DataRow) Then e.Row.Cells(1).Format = "{0:c}" End IfHope this helps, Steve Hi
IN GRID VIEW hi refer this protected decimal totalQTY, totalAMT; protected void ProductList_RowDataBound( object sender, GridViewRowEventArgs e) { DataRowView tableData = e.Row.DataItem as DataRowView; if (e.Row.RowType = = DataControlRowType.Header) { totalQTY = 0; totalAMT = 0; } else if (e.Row.RowType = = DataControlRowType.DataRow) { totalQTY + = ( decimal )tableData[ "QTY" ]; totalAMT + = ( decimal )tableData[ "AMOUNT" ]; } else if (e.Row.RowType DataControlRowType.Footer) { Label tQTY = e.Row.FindControl( "TotalQTY" ) as Label; Label tAMT = e.Row.FindControl( "TotalAMT TemplateField > Implement the RowDataBound decimal totalPrice = 0M; int totalItems = 0; protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType .DataRow) { Label lblPrice = ( Label )e.Row.FindControl( "lblprice" ); decimal price = Decimal .Parse(lblPrice.Text); totalPrice
find that label and set its Text property like this. protected void Griddata_RowDataBound(object sender, GridViewRowEventArgs e) { DataRowView objDRV = (DataRowView)e.Row.DataItem; Label lbl = e.Row.FindControl("lblGender") as Label; if(lbl! = null) { if that Lable, here is the sample code for this protected void GridView_ RowDataBound (object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.DataRow) { string objName = DataBinder.Eval(e.Row.DataItem, "YourColumn").ToString(); ((Button)e.Row.DataItem.FindControl
column containing dates in the gridview. How is this done? Thanks Bill ASP.NET Discussions GridViewRowEventArgs (1) DataControlRowType (1) DataRowView (1) DataRow (1) DateTime (1) RowDataBound (1) IndexOf (1) Hi there Billie, you have to page - - AutoGenerateColumns = "true" OnRowDataBound = "gridView_RowDataBound"> - - end aspx page - - - - code beside - - protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.DataRow) { DataRow row = ((DataRowView)e.Row.DataItem).Row; int index = GetDateColumnIndex(row); e.Row.Cells[index].Text = ((DateTime) row