Hi nabilabolo,
Using the below article ihav created the sample.
In the sample i am checking the Region column for Null value.
When the Region value is null then the value is replaced with City column value.
For this i am making use of WebGridColumn's 3rd optional parameter and using inline expression replacing the column value based on condition.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(entities.Customers.ToList());
}
}
View
@model IEnumerable<WebGrid_Null_Column_Replace_MVC.Customer>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, rowsPerPage: 15);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" media="screen" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css' />
</head>
<body>
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "table table-striped table-hover" },
columns: webGrid.Columns(
webGrid.Column("No.", format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(webGrid.TotalRowCount / webGrid.PageCount) / webGrid.RowsPerPage) * webGrid.RowsPerPage * webGrid.PageIndex),
webGrid.Column("CustomerID", "Id"),
webGrid.Column("ContactName", "Name"),
webGrid.Column("Region", format: (item) => string.IsNullOrEmpty(item.Region) ? item.City : item.Region),
webGrid.Column("City", "City"),
webGrid.Column("Country", "Country")))
</body>
</html>
Screenshot