This is a migrated thread and some comments may be shown as answers.

grid events

2 Answers 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arnaud
Top achievements
Rank 1
Arnaud asked on 27 Jan 2011, 12:34 AM
I have a radgrid which is populated via my vb code below:

Dim Get_Service_Reports As String = "Select * from Service_Reports "  
Get_Service_Reports_Adapter = New SqlDataAdapter(Get_Service_Reports, DBConn)   
Service_Reports_Table =
New DataTable  
Get_Service_Reports_Adapter.Fill(Service_Reports_Table)  
If Not IsPostBack Then
RadGrid1.DataSource = Service_Reports_Table End If

  
RadGrid1.DataBind()


That code executes in the page load.
I then add the following code to give colors to rows depending on certain values

For

 

i As Integer = 0 To RadGrid1.Items.Count - 1
If Service_Reports_Table(i)("Status") = "Newly Submitted" Then
RadGrid1.Items.Item(i).BackColor = Drawing.Color.Wheat
ElseIf Service_Reports_Table(i)("Status") = "Entered" Then
RadGrid1.Items.Item(i).BackColor = Drawing.Color.Thistle
End If
Next

That code right above appears in both the page load and the needdatasource events.
When a user applies a filter or clicks on the edit, update, cancel commands, the colors disappear until a postback occurs.
Can something be done so that the "Code for the colors" execute when those commands are fired?

 

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Jan 2011, 08:49 AM
Hello Arnaud,

 You have populated the grid in PageLoad event. If you are using any advanced feature in grid(like filtering,insert,update,delete operation ), then a better approach is binding the grid using "AdvancedData binding" using NeedDataSource event.

And you can use ItemDataBound event to change the row color depends on some condition. Check out the following code snippet which shows how to achieve this.
VB.NET:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        If item("colorStatus").Text = "True" Then
            'your condition
            item.BackColor = System.Drawing.Color.Red
        Else
            item.BackColor = System.Drawing.Color.Green
        End If
    End If
End Sub


Thanks,
Princy.
0
Arnaud
Top achievements
Rank 1
answered on 27 Jan 2011, 03:43 PM
You were so right. The "ItemDataBound" event had to be set up to apply the effects to the different rows.
Genius Princy. Genius.
Thanks.
Tags
Grid
Asked by
Arnaud
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Arnaud
Top achievements
Rank 1
Share this question
or