jestges
jestges

Reputation: 3740

Handle null values in datagrid

net (c#) and when I try to bind my data to datagrid it is showing some null values in columns (Because there is no data in some columns). But I want to display 0 instead of null in the same column.

how it is possible? Is there any expression?

something like NullDisplayText="0" ??

Thanks

Upvotes: 2

Views: 2786

Answers (3)

Imran Rizvi
Imran Rizvi

Reputation: 7438

There is no such expression in DataGrid, You can write code in ItemDataBound event and replace null with custom text before binding data to cell.

If you are using TemplateColumn and binding data to aspx web controls, you can write Server Side binding for replacing empty or null value with default value

Template Field

<asp:TextBox runat="server" Text='<%#DefaultVal(Eval("FieldName").ToString())% >' ID="txtBox1" ></asp:TextBox>

Server Side property

protected string DefaultVal(string val)
{
    return (val == null)?"0":val;
}

For GridView there is a property for this called NullDisplayText, see following link for detail http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.nulldisplaytext.aspx

Upvotes: 2

Deepesh
Deepesh

Reputation: 5604

It better you handle null values in Sql Queries so that result return does not contain any null values. You can write Sql query for handling Null in this manner

Select isnull(Price,0) from abc

Upvotes: 2

mortb
mortb

Reputation: 9859

You could edit the sql to convert NULL values to zeros using COALESCE

For example:

SELECT COALESCE(potentialNullColumn, 0) FROM table

Hope this helps!

Upvotes: 1

Related Questions