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>
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
Dim m As Date
Dim i As Integer = 0
Using 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 Sub
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.
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 Using
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.ExecuteScalar
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
Member
3 Points
48 Posts
Programmatically changed the forecolor of a field of gridview disappears when clicking on delete...
Nov 08, 2020 05:06 PM|raffarin|LINK
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 = 0
Using 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 Sub
All-Star
53041 Points
23617 Posts
Re: Programmatically changed the forecolor of a field of gridview disappears when clicking on de...
Nov 08, 2020 07:19 PM|mgebhard|LINK
Correct! ViewState maintains the value not the background color.
Member
3 Points
48 Posts
Re: Programmatically changed the forecolor of a field of gridview disappears when clicking on de...
Nov 08, 2020 10:56 PM|raffarin|LINK
Thanks for replying. It's good to know, but my question was how to fix it?
All-Star
53041 Points
23617 Posts
Re: Programmatically changed the forecolor of a field of gridview disappears when clicking on de...
Nov 08, 2020 11:10 PM|mgebhard|LINK
You should set the forecolor in the data binding event not datagrid.load.
Member
3 Points
48 Posts
Re: Programmatically changed the forecolor of a field of gridview disappears when clicking on de...
Nov 09, 2020 11:57 AM|raffarin|LINK
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 Using
Member
3 Points
48 Posts
Re: Programmatically changed the forecolor of a field of gridview disappears when clicking on de...
Nov 09, 2020 01:59 PM|raffarin|LINK
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.ExecuteScalar
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