How to display ‘No data’ when a field value is null

<asp:DataGrid id=’DataGrid1′ OnItemDataBound=ItemDB runat=’server’></asp:DataGrid> VB.NET Private Sub Page_Load(sender As Object, e As System.EventArgs) ’ Put user code to initialize the page here If Not Page.IsPostBack Then ’Populate the DataGrid End If End Sub ’Page_Load Protected Sub ItemDB(ByVal s As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then If e.Item.Cells(1).Text = ‘ ‘ Then e.Item.Cells(1).Text = ‘No data’ End If End If End Sub ’ItemDB C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack ) { //Populate the DataGrid } } protected void ItemDB (Object s , System.Web.UI.WebControls.DataGridItemEventArgs e ) { if ((e.Item.ItemType ==ListItemType.Item) ||(e.Item.ItemType ==ListItemType.AlternatingItem)) { if( e.Item.Cells[1].Text == ‘ ‘) { e.Item.Cells[1].Text = ‘No data’; } } } In Cells[x]/Cells(x) x=> index number

How to use a ButtonColumn in a DataGrid

<asp:DataGrid id=’DataGrid1′ OnItemCommand =’ItemCmd’ AutoGenerateColumns =False runat=’server’> <Columns> <asp:BoundColumn DataField=’ProductID’ HeaderText=’ProductID’></asp:BoundColumn> <asp:BoundColumn DataField=’Productname’ HeaderText=’Productname’></asp:BoundColumn> <asp:ButtonColumn DataTextField=’Productid’ CommandName=’Show’ HeaderText=’Productid’ ButtonType=’PushButton’ Text=’Click’></asp:ButtonColumn> </Columns> </asp:DataGrid> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then ’Populate the DataGrid End If End Sub Protected Sub ItemCmd(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) If e.CommandName = ‘Show’ Then Response.Write(e.Item.Cells(1).Text) End If End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack ) { //Populate the DataGrid } } protected void ItemCmd(Object source , System.Web.UI.WebControls.DataGridCommandEventArgs e ) { if (e.CommandName.ToString () == ‘Show’) { Response.Write(e.Item.Cells[1].Text); } }

How to change the value of a field before it gets displayed in the datagrid

<asp:DataGrid id=’DataGrid1′ OnItemDataBound=’ItemDB’ runat=’server’></asp:DataGrid> VB.NET Protected Sub ItemDB(ByVal s As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Select Case Trim(e.Item.Cells(3).Text) Case ‘Sales Representative’ e.Item.Cells(3).Text = ‘SR’ Case ‘Vice President, Sales’ e.Item.Cells(3).Text = ‘VP’ Case ‘Sales Manager’ e.Item.Cells(3).Text = ‘SM’ Case ‘Inside Sales Coordinator’ e.Item.Cells(3).Text = ‘ISC’ Case Else e.Item.Cells(3).Text = e.Item.Cells(3).Text End Select end if end sub C# protected void ItemDB (Object s , System.Web.UI.WebControls.DataGridItemEventArgs e ) { if ((e.Item.ItemType ==ListItemType.Item) ||(e.Item.ItemType ==ListItemType.AlternatingItem)) { switch ( e.Item.Cells[3].Text.Trim() ) { case ‘Sales Representative’: e.Item.Cells[3].Text = ‘SR’; break; case ‘Vice President, Sales’: e.Item.Cells[3].Text = ‘VP’; break; case ‘Sales Manager’: e.Item.Cells[3].Text = ‘SM’; break; case ‘Inside Sales Coordinator’: e.Item.Cells[3].Text = ‘ISC’; break; default : e.Item.Cells[3].Text = e.Item.Cells[3].Text; break; } } }

How to confirm delete in DataGrid using PushButton

<asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=’False’ OnDeleteCommand =’DelCmd’ OnItemCreated =’ItemCrt’ DataKeyField=’Employeeid’ runat=’server’> <Columns> <asp:ButtonColumn Text=’Delete’ ButtonType=’PushButton’ CommandName=’Delete’></asp:ButtonColumn> <asp:BoundColumn DataField=’firstname’ HeaderText=’First Name’></asp:BoundColumn> </Columns> </asp:DataGrid> VB.NET Dim sqlStmt As String Dim conString As String Dim cn As SqlConnection = Nothing Dim da As SqlDataAdapter = Nothing Dim ds As DataSet Private Sub Page_Load(sender As Object, e As System.EventArgs) conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’ cn = New SqlConnection(conString) If Not Page.IsPostBack Then BindData() End If End Sub ’Page_Load Sub BindData() sqlStmt = ‘select * from emp ‘ ds = New DataSet() da = New SqlDataAdapter(sqlStmt, cn) da.Fill(ds, ‘t1’) DataGrid1.DataSource = ds DataGrid1.DataBind() End Sub ’BindData Protected Sub ItemCrt(sender As Object, e As DataGridItemEventArgs) Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem Dim btn As Button = CType(e.Item.Cells(0).Controls(0), Button) btn.Attributes.Add(‘onclick’, ‘return confirm(’are you sure you want to delete this’)’) Exit End Select End Sub ’ItemCrt Protected Sub DelCmd(sender As [Object], e As DataGridCommandEventArgs) DeleteRow(Me.DataGrid1.DataKeys(e.Item.ItemIndex).ToString()) BindData() End Sub ’DelCmd Private Sub DeleteRow(empid As String) Dim cmd As New SqlCommand(‘DELETE FROM Emp WHERE employeeid =’ + empid, cn) cn.Open() cmd.ExecuteNonQuery() cn.Close() End Sub ’DeleteRow C# string sqlStmt ; string conString ; SqlConnection cn =null; SqlDataAdapter da =null; DataSet ds; private void Page_Load(object sender, System.EventArgs e) { conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’; cn = new SqlConnection(conString); if (!Page.IsPostBack ) { BindData(); } } void BindData() { sqlStmt = ‘select * from emp ‘; ds= new DataSet (); da = new SqlDataAdapter (sqlStmt, cn); da.Fill (ds,’t1’); DataGrid1.DataSource =ds; DataGrid1.DataBind (); } protected void ItemCrt(object sender, DataGridItemEventArgs e) { switch(e.Item.ItemType) { case ListItemType.Item: case ListItemType.AlternatingItem: { Button btn = (Button)e.Item.Cells[0].Controls[0]; btn.Attributes.Add(‘onclick’, ‘return confirm(’are you sure you want to delete this’)’); break; } } } protected void DelCmd(Object sender , DataGridCommandEventArgs e ) { DeleteRow (this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()); BindData(); } private void DeleteRow(string empid) { SqlCommand cmd = new SqlCommand(‘DELETE FROM Emp WHERE employeeid =’+ empid ,cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }