I want to make all of the cell have their own color
eg,
status = v ---> color Yellow
status = P.H ---> Color Green
status = H -----> Blue
else status ----> Red
This is the code of that situation
Protected Sub gvMonthlyReport_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
e.Row.CssClass = "HeaderRow"
e.Row.HorizontalAlign = HorizontalAlign.Center
e.Row.VerticalAlign = VerticalAlign.Middle
End If
'If e.Row.RowType = DataControlRowType.DataRow Then
' e.Row.CssClass = "cellRow"
'End If
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cell As TableCell = e.Row.Cells(17)
Dim status As String = cell.Text
If status = "v" Then
cell.BackColor = Color.Yellow
ElseIf status = "H" Then
cell.BackColor = Color.Blue
ElseIf status = "P.H" Then
cell.BackColor = Color.Green
Else
cell.BackColor = Color.Red
End If
End If
End Sub
this code i have done give me the output color only one column like this
image
output image
actually i want all column , can someone show me?
You have hardcoded cell number 17, but you need to use a loop in the RowDataBound event.
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim i As Integer = 0
Do While (i < e.Row.Cells.Count)
Dim cell As TableCell = CType(e.Row.Cells(i),TableCell)
cell.BackColor = Color.Red
i = (i + 1)
Loop
End If
How to refresh the GridView in onSelectIndexChange of a drop down ?
I want the grid to be refreshed or cleared on every index change
and when the ddlist.selectvalue=0 then a pop up comes saying Please select valid option and then the grid hides.
I am using this code:
Protected Sub ddllist_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddllist.SelectedIndexChanged
If ddllist.SelectedValue = 1 Then
tdtype.InnerHtml = "Leas ID"
btnSave.Enabled = True
trtype.Visible = True
ElseIf (ddllist.SelectedValue = 0) Then
btnSave.Enabled = False
trtype.Visible = False
' Gridconten.DataSource = ""
'Gridconten.DataBind()
'Page_Load(sender, e)
Gridconten.DataSource = Nothing
Gridconten.DataBind()
Else
btnSave.Enabled = True
tdtype.InnerHtml = "Vendor ID"
trtype.Visible = True
End If
End Sub
ok i have got it after some try i am using this !!
basically i am binding it with no data and hiding the grid on index
change
Protected Sub ddllist_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddllist.SelectedIndexChanged
If ddllist.SelectedValue = 1 Then
Label2.Text = ""
tdtype.InnerHtml = "Leas ID"
btnSave.Enabled = True
trtype.Visible = True
Gridconten.DataSource = Nothing
Gridconten.DataBind()
Gridconten.Visible = False
Gridconten.Columns(0).HeaderText = "Lease ID"
ElseIf (ddllist.SelectedValue = 0) Then
btnSave.Enabled = False
trtype.Visible = False
Label2.Text = ""
' Gridconten.DataSource = ""
'Gridconten.DataBind()
'Page_Load(sender, e)
Gridconten.DataSource = Nothing
Gridconten.DataBind()
Gridconten.Visible = False
Else
Label2.Text = ""
btnSave.Enabled = True
tdtype.InnerHtml = "Vendor ID"
trtype.Visible = True
Gridconten.DataSource = Nothing
Gridconten.DataBind()
Gridconten.Visible = False
Gridconten.Columns(0).HeaderText = "Vendor ID"
End If
End Sub
I am trying to display data queried from Oracle in a grid view and have added paging with a PageIndexChanging event. However, after a few different tries with small changes, clicking another page either displays screen with no grid view or just refreshes site with gridview still on page 1. Never worked with web apps/sites before, any ideas?
Code:
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
Dim con As OleDb.OleDbConnection
Dim command As OleDb.OleDbCommand
Dim commandstr As String
Dim wherestr As String
Dim dt As DataTable = New DataTable
Dim oda As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
'Remove any non-aplhanumeric characters from the input string
MeterID_tb.Text = Regex.Replace(MeterID_tb.Text, "[^A-Za-z0-9]+", "")
'Opco_tb.Text = Regex.Replace(Opco_tb.Text, "[^A-Za-z0-9]+", "")
DateFrom_tb.Text = Regex.Replace(DateFrom_tb.Text, "[^A-Za-z0-9]+", "")
DateTo_tb.Text = Regex.Replace(DateTo_tb.Text, "[^A-Za-z0-9]+", "")
'Don't allow the user to search without a filter. The results returned will be too large
'Opco_tb.Text = "" &
If (MeterID_tb.Text = "") And (Division_db.SelectedValue = "Any") And (DateFrom_tb.Text = "") And (DateTo_tb.Text = "") Then
Dim strScript As String = "<script language=JavaScript>"
strScript += "alert(""" & "You must enter at least one search parameter." & """);"
strScript += "</script>"
If (Not Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("clientScript", strScript)
End If
Exit Sub
End If
con = New OleDb.OleDbConnection(*Hidden*)
commandstr = "SELECT METERID as ""Meter ID"", REPID as ""Rep ID"", DIVISION as ""Division"", CITY as ""City"", ADDRESS as ""Address"", RATECODE as ""Ratecode"", METERFORM as ""Meter Form"", METERSTATUS as ""Meter Status"", METERPOINTSTATUS as ""Meter Point Status"", BILLINGCYCLE as ""Billing Cycle"", FILENAME as ""Filename"", FILEDATETIME as ""File Date/Time"" from BAD_METER_LIST"
wherestr = " WHERE"
If MeterID_tb.Text <> "" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " METERID = '" + MeterID_tb.Text + "'"
Else
wherestr = wherestr + " AND METERID = '" + MeterID_tb.Text + "'"
End If
End If
If Division_db.SelectedValue <> "Any" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " DIVISION = " + Division_db.SelectedValue
Else
wherestr = wherestr + " AND DIVISION = " + Division_db.SelectedValue
End If
End If
If DateFrom_tb.Text <> "" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
Else
wherestr = wherestr + " AND FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
End If
End If
If DateTo_tb.Text <> "" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
Else
wherestr = wherestr + " AND FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
End If
End If
command = New OleDb.OleDbCommand(commandstr + wherestr)
command.Connection = con
con.Open()
oda.SelectCommand = command
oda.Fill(dt)
Me.Grid_Bad_Meters.DataSource = dt
Me.Grid_Bad_Meters.DataBind()
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
Protected Sub Grid_Bad_Meters_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles Grid_Bad_Meters.PageIndexChanging
'Grid_Bad_Meters.Visible = True
Grid_Bad_Meters.PageIndex = e.NewPageIndex
Grid_Bad_Meters.DataBind()
End Sub
End Class
You need to rebind the GridView upon changing PageIndex.
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
#Region "Subs"
Private Sub CleanInput()
'Remove any non-aplhanumeric characters from the input string
MeterID_tb.Text = Regex.Replace(MeterID_tb.Text, "[^A-Za-z0-9]+", "")
'Opco_tb.Text = Regex.Replace(Opco_tb.Text, "[^A-Za-z0-9]+", "")
DateFrom_tb.Text = Regex.Replace(DateFrom_tb.Text, "[^A-Za-z0-9]+", "")
DateTo_tb.Text = Regex.Replace(DateTo_tb.Text, "[^A-Za-z0-9]+", "")
End Sub
Private Sub RequireFilter
'Don't allow the user to search without a filter. The results returned will be too large
'Opco_tb.Text = "" &
If ((MeterID_tb.Text = "")
AndAlso (Division_db.SelectedValue = "Any")
AndAlso (DateFrom_tb.Text = "")
AndAlso (DateTo_tb.Text = "")) Then
Dim strScript As String = "<script language=JavaScript>"
strScript += "alert(""" & "You must enter at least one search parameter." & """);"
strScript += "</script>"
If (Not Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("clientScript", strScript)
End If
Exit Sub
End If
End Sub
Private Sub FillGridView
Dim con As OleDb.OleDbConnection
Dim command As OleDb.OleDbCommand
Dim commandstr As String
Dim wherestr As String
Dim dt As DataTable = New DataTable
Dim oda As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
con = New OleDb.OleDbConnection(*Hidden*)
commandstr = "SELECT METERID as ""Meter ID"", REPID as ""Rep ID"", DIVISION as ""Division"", CITY as ""City"", ADDRESS as ""Address"", RATECODE as ""Ratecode"", METERFORM as ""Meter Form"", METERSTATUS as ""Meter Status"", METERPOINTSTATUS as ""Meter Point Status"", BILLINGCYCLE as ""Billing Cycle"", FILENAME as ""Filename"", FILEDATETIME as ""File Date/Time"" from BAD_METER_LIST"
wherestr = " WHERE"
If MeterID_tb.Text <> "" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " METERID = '" + MeterID_tb.Text + "'"
Else
wherestr = wherestr + " AND METERID = '" + MeterID_tb.Text + "'"
End If
End If
If Division_db.SelectedValue <> "Any" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " DIVISION = " + Division_db.SelectedValue
Else
wherestr = wherestr + " AND DIVISION = " + Division_db.SelectedValue
End If
End If
If DateFrom_tb.Text <> "" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
Else
wherestr = wherestr + " AND FILEDATETIME >= to_date('" & DateFrom_tb.Text & "', 'mmddyyyy')"
End If
End If
If DateTo_tb.Text <> "" Then
If wherestr = " WHERE" Then
wherestr = wherestr + " FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
Else
wherestr = wherestr + " AND FILEDATETIME <= to_date('" & DateTo_tb.Text & "', 'mmddyyyy')"
End If
End If
command = New OleDb.OleDbCommand(commandstr + wherestr)
command.Connection = con
con.Open()
oda.SelectCommand = command
oda.Fill(dt)
Me.Grid_Bad_Meters.DataSource = dt
Me.Grid_Bad_Meters.DataBind()
End Sub
#End Region
Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
CleanInput()
RequireFilter()
FillGridView()
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
Protected Sub Grid_Bad_Meters_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles Grid_Bad_Meters.PageIndexChanging
'Grid_Bad_Meters.Visible = True
Grid_Bad_Meters.PageIndex = e.NewPageIndex
Grid_Bad_Meters.DataBind()
FillGridView()
End Sub
End Class
I got a problem which is not an error in writing data to Excel file from VB.NET.
The code to save data in Excel file is as the following:
Sub WriteToExcel(ByVal dt As DataTable)
Dim excel_app As New Excel.ApplicationClass()
excel_app.Visible = True
Dim workbook As Excel.Workbook = excel_app.Workbooks.Open(Filename:=FileUpload1.PostedFile.FileName)
Dim sheet_name As String = "rptSyncADandITSM"
Dim sheet As Excel.Worksheet = FindSheet(workbook, sheet_name)
sheet.Cells(1, "AF") = "Status"
sheet.Cells(1, "AG") = "Message"
sheet.Range("AF" & 1).Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AF1").Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AF1").ColumnWidth = 10
sheet.Range("AG1").Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AG1").Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AG1").ColumnWidth = 15
Dim header_range As Excel.Range = sheet.Range("AF1", "AG1")
header_range.Font.Bold = True
header_range.Font.Color = System.Drawing.ColorTranslator.ToOle(Drawing.Color.Black)
Dim borders As Excel.Borders = header_range.Borders
borders.LineStyle = Excel.XlLineStyle.xlContinuous
borders.Weight = 1.9R
For Each dr As DataRow In dt.Rows
Dim value_range As Excel.Range = sheet.Range("AF" & dr(0).ToString, "AG" & dr(0).ToString)
If Not dr.IsNull(0) Then
sheet.Range("AF" & dr(0).ToString).Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AF" & dr(0).ToString).Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AF" & dr(0).ToString).ColumnWidth = 10
sheet.Range("AG" & dr(0).ToString).Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AG" & dr(0).ToString).Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AG" & dr(0).ToString).ColumnWidth = 15
Dim values(0, 1) As String
values(0, 0) = dr(1).ToString
values(0, 1) = dr(2).ToString
value_range.Value2 = values
End If
Dim value_borders As Excel.Borders = value_range.Borders
value_borders.LineStyle = Excel.XlLineStyle.xlContinuous
value_borders.Weight = 2.0R
Next
workbook.Save()
workbook.Close()
'workbook.Close(SaveChanges:=True)
excel_app.Quit()
End Sub
I reference these codes from this
But it only works when i debug the program. In runtime(not debugging) doesn't work to save.
i created a simple advanced search page for web application, i thought sharing it with you might help beginners
the following is an example of an advanced search page for an employee database using VB.Net
the following is the code behind page
Imports System.Data.OleDb
Partial Class searchme
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mydb As New OleDbConnection
mydb = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= |datadirectory|employee.mdb;Persist Security Info=True")
mydb.Open()
Dim sqlstring = "select * from [dataview] where "
If MRNTextBox1.Text <> "" Then sqlstring = sqlstring + "[code] like '%" + CodeNameTextBox1.Text + "%' OR [EmployeeName] like '%" + CodeNameTextBox1.Text + "%' AND "
If GOVDDL.SelectedItem.Text <> "--Please Select--" Then sqlstring = sqlstring + "[Governorate] ='" + GOVDDL.SelectedItem.Text + "' AND "
If genderddl.SelectedItem.Text <> "--Please Select--" Then sqlstring = sqlstring + "[Gender] ='" + genderddl.SelectedItem.Text + "' AND "
If DateEmploymentFrom.Text <> "" And DateEmploymentTo.Text <> "" Then sqlstring = sqlstring + "[DateEmployment] >= #" + DatumKonvert1.DK1(DateEmploymentFrom.Text) + "# AND [Datepresentation] <= #" + DatumKonvert1.DK1(DateEmploymentTo.Text) + "# AND "
If DepartmentDDL.SelectedItem.Text <> "--Please Select--" Then sqlstring = sqlstring + "[Department] ='" + DepartmentDDL.SelectedItem.Text + "' AND "
sqlstring = Left(sqlstring, Len(sqlstring) - 5) + " order by " + OrderByDDL.SelectedItem.Text
Dim myds As New AccessDataSource
myds.DataFile = "~\App_Data\employee.mdb"
myds.SelectCommand = sqlstring
' Dim Mygrid As New GridView
Mygrid.DataSource = myds
Mygrid.DataBind()
' Me.form1.Controls.Add(Mygrid)
mydb.Close()
RecCount.Text = "Filtered Record Count = " + mygrid.Rows.Count.ToString
Session("dsource") = myds
Response.Redirect("sresults.aspx")
End Sub
End Class
you did a good job, also try the following
link text
link text