Answered by:
Programmatically changed the forecolor of a field of gridview disappears when clicking on delete button

Question
-
User-427368358 posted
Hi
I changed programmatically the forecolor of a field of gridview in red when the date of a field is before today. It works but when i click on the delete button, the forecolor disappears. Enableviewstate = true.
It has to do with postback caused by the delete button, but how to fix this?
Thanks
Raf
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:lening %>"
DeleteCommand="DELETE FROM [lening] WHERE [leningnr] = @leningnr"
SelectCommand="SELECT * FROM [lening] order by enddat"
<DeleteParameters><asp:Parameter Name="leningnr" Type="Int32" /></DeleteParameters><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"DataKeyNames="leningnr" EnableViewState="true" DataSourceID="SqlDataSource1" />
<asp:LinkButton ID="ann" runat="server" CausesValidation="false" OnClientClick="return confirm('sure?');"
CommandName="Delete" Text="delete"></asp:LinkButton>Code-behind:
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
Dim m As Date
Dim i As Integer = 0Using mConnection As New SqlConnection(param.ConnectionString)
mConnection.Open()
sql = "select enddat from lening order by enddat "
comd = New SqlCommand(sql, mConnection)
dtreader = comd.ExecuteReader
If dtreader.HasRows Then
While dtreader.Read()
m = dtreader.GetDateTime(0)
If m < DateTime.Today Then
GridView1.Rows(i).Cells(9).ForeColor = Drawing.Color.Red
End If
i = i + 1
End While
End If
dtreader.Close()
mConnection.Close()
End Using
End SubSunday, November 8, 2020 5:06 PM
Answers
-
User-427368358 posted
Hi
i changed a little bit my code and now it works. Thanks
Best regards
Raf
sql = "select count(enddat) from lening"
comd = New SqlCommand(sql, mConnection)
rec = comd.ExecuteScalarFor i = 0 To rec - 1
dtreader.Read()
m = dtreader.GetDateTime(0)
If m < DateTime.Today Then
GridView1.Rows(i).Cells(9).ForeColor = Drawing.Color.Red
End If
Next- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 9, 2020 1:59 PM
All replies
-
User475983607 posted
I changed programmatically the forecolor of a field of gridview in red when the date of a field is before today. It works but when i click on the delete button, the forecolor disappears. Enableviewstate = true.Correct! ViewState maintains the value not the background color.
Sunday, November 8, 2020 7:19 PM -
User-427368358 posted
Thanks for replying. It's good to know, but my question was how to fix it?
Sunday, November 8, 2020 10:56 PM -
User475983607 posted
Thanks for replying. It's good to know, but my question was how to fix it?You should set the forecolor in the data binding event not datagrid.load.
Sunday, November 8, 2020 11:10 PM -
User-427368358 posted
I changed a little bit my code (which works with datagrid.load) and I have tried in GridView1_DataBinding but then i get the error:
The index was out of range. It must not be negative and must be less than the size of the collection.
Using mConnection As New SqlConnection(param.ConnectionString)
mConnection.Open()
sql = "select count(enddat) from lening"
comd = New SqlCommand(sql, mConnection)
rec = comd.ExecuteScalar
sql = "select enddat from lening order by enddat "
comd = New SqlCommand(sql, mConnection)
dtreader = comd.ExecuteReader
For i = 0 To rec - 1
dtreader.Read()
m = dtreader.GetDateTime(0)
If m < DateTime.Today Then
GridView1.Rows(i).Cells(9).ForeColor = Drawing.Color.Red
End If
Next
dtreader.Close()
mConnection.Close()
End UsingMonday, November 9, 2020 11:57 AM -
User-427368358 posted
Hi
i changed a little bit my code and now it works. Thanks
Best regards
Raf
sql = "select count(enddat) from lening"
comd = New SqlCommand(sql, mConnection)
rec = comd.ExecuteScalarFor i = 0 To rec - 1
dtreader.Read()
m = dtreader.GetDateTime(0)
If m < DateTime.Today Then
GridView1.Rows(i).Cells(9).ForeColor = Drawing.Color.Red
End If
Next- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 9, 2020 1:59 PM