By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,602 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,602 IT Pros & Developers. It's quick & easy.

Refreshing grid

P: 65
How can i refresh datagrid in vb.net.
I want to display color in one cell in grid but when it loads first time it is working here is my code
Expand|Select|Wrap|Line Numbers
  1. Public Class Dialog_Colorazione
  2.     Const YEAR_START As Integer = 2005
  3.     'function to fill the combo with year
  4.     Private Sub LoadComboBoxes()
  5.         cboYear.Items.Clear()
  6.         cboYear.SelectedIndex = -1
  7.         Dim i As Integer
  8.         For i = Date.Today.Year To YEAR_START Step -1
  9.             cboYear.Items.Add(i.ToString())
  10.         Next
  11.         cboYear.SelectedIndex = 0
  12.         ' loadcolor(cboYear.SelectedItem)
  13.     End Sub
  14.  
  15.     Private Sub Dialog_Colorazione_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  16.         'Me.Close()
  17.     End Sub
  18.     'till here function to fill the combo with year
  19.  
  20.    Private Sub Dialog_Colorazione_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  21.         'calling functions to fill the combo and display the color details
  22.         'cboYear.SelectedText = Today.Year
  23.  
  24.         LoadComboBoxes()
  25.         ' loadcolor(cboYear.SelectedItem)
  26.         'cboYear.SelectedIndex = 1
  27.         dgvFatturato.Refresh()
  28.  
  29.     End Sub
  30.  
  31.     'function the collect and fill the grid with the colors in the selected year
  32.     Public Function loadcolor(ByVal year As Integer)
  33.         Dim currentyear As Integer = Today.Year
  34.         Using connection As New OleDbConnection(My.Settings.BCS_ACCConnectionString & ";Jet OLEDB:Database Password=" & Costanti.strAccessDbPassword)
  35.  
  36.             connection.Open()
  37.  
  38.             Dim adpt As New OleDb.OleDbDataAdapter("SELECT RefYear,LowRange,UpRange,ColorName FROM ACC_COLORI_FATTURATO WHERE RefYear=" & year & "", connection)
  39.  
  40.             Dim ds As New DataTable
  41.  
  42.             adpt.Fill(ds)
  43.  
  44.             'added on 08/12/2007 to display color in the datagrid
  45.             If ds Is Nothing Or ds.Rows.Count = 0 Then
  46.                 ShowMessage("Nessun dato per i criteri selezionati")
  47.                 dgvFatturato.Visible = False
  48.             Else
  49.                 dgvFatturato.DataSource = Nothing
  50.                 dgvFatturato.Visible = True
  51.                 dgvFatturato.DataSource = ds
  52.  
  53.                 ds.Columns(0).ColumnName = "Anno"
  54.                 ds.Columns(1).ColumnName = "Maggiore da"
  55.                 ds.Columns(2).ColumnName = "Minore da"
  56.                 ds.Columns(3).ColumnName = "Colore"
  57.                 ' ds.Columns(4).ColumnName = "ID colore" 'commended on 11/01/2008 for hiding the coloumn
  58.  
  59.                 'Added on 11/01/2008
  60.                 ' Dim year1 As Integer = cboYear.SelectedItem
  61.                 Using connection1 As New OleDbConnection(My.Settings.BCS_ACCConnectionString & ";Jet OLEDB:Database Password=" & Costanti.strAccessDbPassword)
  62.                     connection1.Open()
  63.                     Dim ds1 As New DataTable
  64.                     Dim adpt1 As New OleDbDataAdapter("select color from ACC_COLORI_FATTURATO WHERE RefYear=" & year & "", connection)
  65.                     adpt1.Fill(ds1)
  66.                     If dgvFatturato.RowCount > 0 And ds1.Rows.Count > 0 Then
  67.                         For Each row As DataGridViewRow In dgvFatturato.Rows
  68.                             ' For i As Integer = 0 To ds1.Rows.Count - 1
  69.                             Dim colorn As Integer = ds1.Rows(row.Index).Item(0)
  70.                             row.Cells(3).Style.BackColor = Color.FromArgb(255, Color.FromArgb(CType(colorn, Int32)))
  71.                         Next
  72.                     End If
  73.                     connection1.Close()
  74.                 End Using
  75.                 ''Till here
  76.  
  77.             End If
  78.             'Till here added on 08/12/2007  to display color in the datagrid
  79.  
  80.             connection.Close()
  81.         End Using
  82.  
  83.     End Function
  84.     'till here function the collect and fill the grid with the colors in the selected year
  85.  
  86.     'function to convert decimal value to hexadecimal
  87.     Private Function DeciamlToHexadeciaml(ByVal number As Integer) As String
  88.         Dim hexvalues As String() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
  89.         Dim result As String = Nothing
  90.         Dim final As String = Nothing
  91.         Dim div As Integer = 0
  92.         Dim rem1 As Integer
  93.  
  94.         While True
  95.             rem1 = (number Mod 16)
  96.             result += hexvalues(rem1).ToString()
  97.  
  98.             If number < 16 Then
  99.                 Exit While
  100.             End If
  101.  
  102.             number = (number / 16)
  103.         End While
  104.         For i As Integer = (result.Length - 1) To 0 Step -1
  105.  
  106.             final += result(i)
  107.         Next
  108.  
  109.         Return final
  110.     End Function
  111.     'till here function to convert decimal value to hexadecimal
  112.  
  113.     'to collect and display the color information on change of the combo
  114.     Private Sub cboYear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboYear.SelectedIndexChanged
  115.         loadcolor(cboYear.SelectedItem)
  116.     End Sub
  117.     'till here to collect and display the color information on change of the combo
  118.  
  119.     Protected Sub ShowMessage(ByVal message As String)
  120.         ToolStripStatusLabel2.Text = message
  121.     End Sub
  122.  
  123.     Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
  124.         Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
  125.         Me.Close()
  126.     End Sub
  127.     Private Sub dgvFatturato_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFatturato.CellContentClick
  128.  
  129.     End Sub
  130.  
  131.     Protected Overrides Sub Finalize()
  132.         MyBase.Finalize()
  133.     End Sub
  134. End Class
  135.  


Thanks....
Jan 11 '08 #1
Share this Question
Share on Google+
3 Replies

Frinavale
Expert Mod 5K+
P: 9,735
After reading your question, I got the impression that you were having problems changing the colour of your GridView's cells when the GridView is First Loaded.

I suggest changing the cell's colours in the method that handles your GridView's RowDataBound event.

EG:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub dgvFatturato_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV_SearchResults.RowDataBound
  2.        For i As Integer = 0 to e.Row.Cells.Count -1
  3.             e.Row.Cells(i).Style.BackColor = Color.FromArgb(255, Color.FromArgb(CType(colorn, Int32)))
  4.         Next
  5.  
  6. End Sub
  7.  
I'm not sure if this is what your asking for though...your code suggests that you are dealing with selecting colours in your GridView...

-Frinny
Jan 11 '08 #2

P: 65
Thank you.. for your replay..


My problem is :
When the page loads first time it display color in the grid. But after that it is not displaying color on page load and in dropdown selected index change i am calling the same function there it is working fine.
Jan 12 '08 #3

Frinavale
Expert Mod 5K+
P: 9,735
Thank you.. for your replay..


My problem is :
When the page loads first time it display color in the grid. But after that it is not displaying color on page load and in dropdown selected index change i am calling the same function there it is working fine.
Something else (after your Page_Load) may be setting the colour to something else...Check that this is not the case.
Jan 12 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.