how replace blank in place of zero for particular column in a datatable dt in asp.net
How replace blank in place of zero for particular column in asp.net
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
how replace blank in place of zero for particular column in a datatable dt in asp.net
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Dec 2, 2022, 6:20 AM EST
Replace Empty with Zero (0) string.IsNullOrEmpty(Convert.ToString(row[columnName])) ? "0" : row[columnName];Sandeep KumarRank #1,642 · 937 reputationPosted Dec 2, 2022, 6:35 AM EST
Where I have to change For Particular Column
dt = (dt.AsEnumerable().Select(delegate (DataRow r)
{
DataRow nr = dt.NewRow();
foreach (DataColumn item in dt.Columns)
{
nr.SetField(item.ColumnName, r[item.ColumnName].ToString() == "0" ? null : r[item.ColumnName]);
}
return nr;
})).CopyToDataTable();
Vishal YelveRank #178 · 13.6K reputationPosted Dec 2, 2022, 7:09 AM EST
try this
private void dtChangeZeroToNull (DataTable dataTable){ List dcNames = dataTable.Columns .Cast() .Select(x => x.ColumnName) .ToList(); //This querying of the Column Names, you could do with LINQ foreach (DataRow row in dataTable.Rows) //This is the part where you update the cell one by one foreach (string columnName in dcNames) row[columnName] = (int)row[columnName] == 0 ? DBNull.Value : row[columnName]; }