UserId
|
UserName
|
Education
|
Location
|
1
|
SureshDasari
|
Chennai
|
|
2
|
MadhavSai
|
MBA
|
|
3
|
MaheshDasari
|
B.Tech
|
Nuzividu
|
4
|
Mahendra
|
CA
|
for (int i = 0; i <
dt.Rows.Count;i++ )
{
for(int
j=0;j<dt.Columns.Count;j++)
{
if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
// Write your Custom Code
dt.Rows[i][j] = "your
required value";
}
}
}
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Find Null Values in datatable and replace with other values
in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:GridView ID="gvDetails"
runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
|
using System;
using System.Data;
|
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
/// <summary>
/// Dynamically create & bind data to
datatable and bind datatable to gridview
/// </summary>
protected void
BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId",
typeof(Int32));
dt.Columns.Add("UserName",
typeof(string));
dt.Columns.Add("Education",
typeof(string));
dt.Columns.Add("Location",
typeof(string));
DataRow dtrow = dt.NewRow(); // Create New
Row
dtrow["UserId"]
= 1; //Bind Data to Columns
dtrow["UserName"]
= "SureshDasari";
dtrow["Education"]
= null;
dtrow["Location"]
= "Chennai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["UserId"]
= 2; //Bind Data to Columns
dtrow["UserName"]
= "MadhavSai";
dtrow["Education"]
= "MBA";
dtrow["Location"]
= string.Empty;
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["UserId"]
= 3; //Bind
Data to Columns
dtrow["UserName"]
= "MaheshDasari";
dtrow["Education"]
= "B.Tech";
dtrow["Location"]
= "Nuzividu";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["UserId"]
= 4; //Bind Data to Columns
dtrow["UserName"]
= "Mahendra";
dtrow["Education"]
= "CA";
dtrow["Location"]
= null;
dt.Rows.Add(dtrow);
for (int i = 0; i <
dt.Rows.Count;i++ )
{
for(int
j=0;j<dt.Columns.Count;j++)
{
if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
// Write your Custom Code
dt.Rows[i][j] = "N/A";
}
}
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
|
Imports System.Data
Partial Class
Default2
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridviewData()
End If
End Sub
Protected Sub
BindGridviewData()
Dim dt As New DataTable()
dt.Columns.Add("UserId",
GetType(Int32))
dt.Columns.Add("UserName",
GetType(String))
dt.Columns.Add("Education",
GetType(String))
dt.Columns.Add("Location",
GetType(String))
Dim dtrow As DataRow =
dt.NewRow()
' Create New Row
dtrow("UserId")
= 1
'Bind Data to Columns
dtrow("UserName")
= "SureshDasari"
dtrow("Education")
= Nothing
dtrow("Location")
= "Chennai"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 2
'Bind Data to Columns
dtrow("UserName")
= "MadhavSai"
dtrow("Education")
= "MBA"
dtrow("Location")
= String.Empty
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 3
'Bind Data to Columns
dtrow("UserName")
= "MaheshDasari"
dtrow("Education")
= "B.Tech"
dtrow("Location")
= "Nuzividu"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 4
'Bind Data to Columns
dtrow("UserName")
= "Mahendra"
dtrow("Education")
= "CA"
dtrow("Location")
= Nothing
dt.Rows.Add(dtrow)
For i As Integer = 0 To
dt.Rows.Count - 1
For j As Integer = 0 To
dt.Columns.Count - 1
If String.IsNullOrEmpty(dt.Rows(i)(j).ToString())
Then
' Write your Custom Code
dt.Rows(i)(j) = "N/A"
End If
Next
Next
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
End Class
|
UserId
|
UserName
|
Education
|
Location
|
1
|
SureshDasari
|
N/A
|
Chennai
|
2
|
MadhavSai
|
MBA
|
N/A
|
3
|
MaheshDasari
|
B.Tech
|
Nuzividu
|
4
|
Mahendra
|
CA
|
N/A
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
1 comments :
Thank you very much! I'm looking for this