1.How To Set a Date Format In GridView Using ASP.NET or How to format datetime in gridview boundfield and template columns
<Columns>
<asp:BoundField DataField="Date1" HeaderText="Date1" HtmlEncode="false" DataFormatString="{0:s}" />
<asp:BoundField DataField="Date2" HeaderText="Date2" HtmlEncode="false" DataFormatString="{0:D}" />
<asp:BoundField DataField="Date3" HeaderText="Date3" HtmlEncode="false" DataFormatString="{0:m}" />
<asp:BoundField DataField="Date4" HeaderText="Date4" HtmlEncode="false" DataFormatString="{0:d}" />
<asp:BoundField DataField="Total" HeaderText="Total" HtmlEncode="false" DataFormatString="{0:C2}" />
</Columns>
<Columns>
<asp:BoundField DataField="Date1" HeaderText="Date1" HtmlEncode="false" DataFormatString="{0:s}" />
<asp:BoundField DataField="Date2" HeaderText="Date2" HtmlEncode="false" DataFormatString="{0:D}" />
<asp:BoundField DataField="Date3" HeaderText="Date3" HtmlEncode="false" DataFormatString="{0:m}" />
<asp:BoundField DataField="Date4" HeaderText="Date4" HtmlEncode="false" DataFormatString="{0:d}" />
<asp:BoundField DataField="Total" HeaderText="Total" HtmlEncode="false" DataFormatString="{0:C2}" />
</Columns>
How to make Gridview commandfield edit visible= false in runtime?
Dim objCmd As CommandField = New CommandField()
objCmd = grdBMSearch.Columns(8)
objCmd.ShowEditButton = False
objCmd = grdBMSearch.Columns(8)
objCmd.ShowEditButton = False
Export To Excel in color VB.Net
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
ExportToExcel()
End Sub
Public Sub ExportToExcel()
'If dt.Rows.Count > 0 Then
Dim filename As String = "BoardMemberDetail.xls"
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim dgGrid As New GridView
Session("BMDetail") = GetSearchResults()
dgGrid.DataSource = GetSearchResults()
dgGrid.DataBind()
Dim i As Integer = 0
Do While (i < dgGrid.Rows.Count)
Dim value As String = dgGrid.Rows(i).Cells(2).Text
If value = "0" Then
dgGrid.Rows(i).Cells(2).Text = "N/A"
End If
dgGrid.Rows(i).Cells(0).Visible = False
i = (i + 1)
Loop
dgGrid.HeaderRow.Cells(0).Visible = False
'dgGrid.HeaderRow.Style.Add("background-color", "#538DD5")
'dgGrid.HeaderRow.ForeColor = Color.White
Dim columncount As Integer = dgGrid.Rows(0).Cells.Count
For p As Integer = 0 To columncount - 1
dgGrid.HeaderRow.Cells(p).Style.Add("background-color", "#538DD5")
dgGrid.HeaderRow.ForeColor = Color.White
Next
'dgGrid.HeaderRow.Cells(columncount - 1).Style.Add("background-color", "#538DD5")
'dgGrid.HeaderRow.ForeColor = Color.White
Dim j As Integer = 1
'This loop is used to apply stlye to cells based on particular row
For Each gvrow As GridViewRow In dgGrid.Rows
gvrow.BackColor = Color.White
If j <= dgGrid.Rows.Count Then
If j Mod 2 <> 0 Then
For a As Integer = 0 To gvrow.Cells.Count - 1
gvrow.Cells(a).Style.Add("background-color", "#EFF3FB")
Next
End If
End If
j += 1
Next
''Get the HTML for the control.
dgGrid.RenderControl(hw)
'Write the HTML back to the browser.
'Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename & "")
Me.EnableViewState = False
Response.Write(tw.ToString())
Response.End()
End Sub
ExportToExcel()
End Sub
Public Sub ExportToExcel()
'If dt.Rows.Count > 0 Then
Dim filename As String = "BoardMemberDetail.xls"
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim dgGrid As New GridView
Session("BMDetail") = GetSearchResults()
dgGrid.DataSource = GetSearchResults()
dgGrid.DataBind()
Dim i As Integer = 0
Do While (i < dgGrid.Rows.Count)
Dim value As String = dgGrid.Rows(i).Cells(2).Text
If value = "0" Then
dgGrid.Rows(i).Cells(2).Text = "N/A"
End If
dgGrid.Rows(i).Cells(0).Visible = False
i = (i + 1)
Loop
dgGrid.HeaderRow.Cells(0).Visible = False
'dgGrid.HeaderRow.Style.Add("background-color", "#538DD5")
'dgGrid.HeaderRow.ForeColor = Color.White
Dim columncount As Integer = dgGrid.Rows(0).Cells.Count
For p As Integer = 0 To columncount - 1
dgGrid.HeaderRow.Cells(p).Style.Add("background-color", "#538DD5")
dgGrid.HeaderRow.ForeColor = Color.White
Next
'dgGrid.HeaderRow.Cells(columncount - 1).Style.Add("background-color", "#538DD5")
'dgGrid.HeaderRow.ForeColor = Color.White
Dim j As Integer = 1
'This loop is used to apply stlye to cells based on particular row
For Each gvrow As GridViewRow In dgGrid.Rows
gvrow.BackColor = Color.White
If j <= dgGrid.Rows.Count Then
If j Mod 2 <> 0 Then
For a As Integer = 0 To gvrow.Cells.Count - 1
gvrow.Cells(a).Style.Add("background-color", "#EFF3FB")
Next
End If
End If
j += 1
Next
''Get the HTML for the control.
dgGrid.RenderControl(hw)
'Write the HTML back to the browser.
'Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename & "")
Me.EnableViewState = False
Response.Write(tw.ToString())
Response.End()
End Sub