views:

1272

answers:

2

Hi i have dataset having one table have 5 columns fill data from database.

When i run application datatable contains more than 50 rows, I would like to update value of datatable after getting data from databse. My requirements are

  1. This table have few cells having Null value, I would like to replace null with "-" ?
  2. One column is System.datetime datatype, where i would like to remove time. I would only like to have date?

I am using this dataset in crystal report of asp.net using PUSH approach.

Here i am thinking that i apply one loop of rows present in datatable and update cells accordingly. But i am searching any direct update method?

Please help me how can i solve above 2 issue?

A: 

One part of your question can be done with sql in the database where u just do an update statement and replace those empty cells with a "-". The other part of your question did not become very clear to me. If you asked how to make sure you can edit rows in a datacolumn using a datagrid you can just read this article and you will be able to make the grid go into editable mode.

Younes
Thanks,Can't we replace all null value in datatable with single characters
Hemant Kothiyal
A: 

Looping through the rows, updating the values as you go is definitely a solution, and a quite easy one:

foreach (DataRow row in table.Rows)
{
    if (row.IsNull("foo")) row["foo"] = "-";
    if (row.IsNull("bar")) row["bar"] = "-";
    row["date"] = ((DateTime)row["date"]).Date;
}

Alternatively, you could create new columns in the table, using expressions to autogenerate content:

table.Columns.Add("foo_dash", typeof(string), "IsNull(foo, '-')");
table.Columns.Add("bar_dash", typeof(string), "IsNull(bar, '-')");

(I don't know the date functions in ADO.NET so you will have to figure the last one out yourself.)

You have tagged your post ASP.NET, so I guess it is reasonable to assume that you are going to bind your DataTable to some multi-record data control (GridView, Repeater, etc). If this is the case, it might be better to do the transformations during databinding instead:

protected void theGrid_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    var data = e.DataItem as DataRowView;
    if (data != null)
    {
        if (data.Row.IsNull("foo")) e.Row.Cells[0] = "-";
        if (data.Row.IsNull("bar")) e.Row.Cells[0] = "-";
    }
}

While this seems to require a bit more code, it also gives you more flexibility. Example:

if (data.Row.IsNull("importantField")) e.Row.CssClass = "error";

In a GridView, the date can be formatted using a DataFormatString in the column declaration:

<asp:BoundField DataField="data" DataFormatString="{0:d}" />

Similar when databinding a Repeater:

<%# Eval("date", "{0:d}") %>
Jørn Schou-Rode
Thanks,Actually i am using PUSH approach in crystal report where i am using dataset columns in a report.
Hemant Kothiyal
OK. I know nothing about Crystal Reports, but I guess the first two solutions should work as long as you have a reference to a `DataTable`.
Jørn Schou-Rode