How to change dataset column value in asp.net

Hi, I have a dataset that has one table has 5 columns, fills the data from the database.

When I run the datatable application contains more than 50 rows, I would like to update the datatable value after getting the data from the database. My requirements

  • There are several cells with zero value in this table, I would like to replace null with "-"?
  • One column is the System.datetime data type, where I would like to remove the time. I would only like to have a date?

I use this dataset in an asp.net crystal report using the PUSH approach.

Here I think that I am applying one loop of rows present in datatable and update cells respectively. But am I looking for any direct update method?

Please help me, how can I solve the problem above 2?

+4
source share
2 answers

Quoting along the lines, updating values ​​along the way, is certainly a solution and quite simple:

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 can create new columns in the table using expressions to automatically create 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 need to display the last one.)

You tagged your ASP.NET post, so I assume it is reasonable to assume that you are going to bind your DataTable to some data management with several records ( GridView , Repeater , etc.). If so, it would be better to do the transformations during data binding:

 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] = "-"; } } 

Although this requires a bit more code, it also gives you more flexibility. Example:

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

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

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

Similarly when data binding a Repeater :

 <%# Eval("date", "{0:d}") %> 
+3
source

One part of your question can be done using sql in the database, where u just executes the update statement and replaces these empty cells with β€œ-”. The other part of your question has not become very clear to me. If you asked how to make sure that you can edit rows in a datacolumn using a datagrid, you can just read this article and you can make the grid go into editable mode.

0
source

Source: https://habr.com/ru/post/1300037/


All Articles