We have seen many examples on how to change the background color of row based on the data using DataGrid Control. This can be done using a helper function or using the appropriate event of the datagrid
We will use the similar scenerio to find out how to use GridView Control in such case.
Step 1: Drag Drop GridView Control
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" ></asp:GridView>
Step 2: We will use SqlDataSource control for the connectivity with the Sql Database
Lets use web.config for Sql Connection string:
<connectionStrings>
connectionStrings><add name="DummyDB" connectionString="Server=localhost;Integrated Security=True;Database=DummyDB;Persist Security Info=True" providerName="System.Data.SqlClient" /></connectionStrings >
</connectionStrings >
The connection string is used with the Sql Datasource control as follows in the respective page
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DummyDB %>" SelectCommand="Select * from products"></asp:SqlDataSource>Step 3: We have Products table with field Unitprice as one of the fields. We will change the row background color to red if the UnitPrice<10
Step 3: We have Products table with field Unitprice as one of the fields. We will change the row background color to red if the UnitPrice<10
This is done using the RowDataBound event
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >
</asp:GridView>
Step 4: We will code as follows for the RowDataBound Event.
VB.NET
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.DataItem("UnitsInStock") < 20) Then
e.Row.BackColor = Drawing.Color.Red
End If
End If
End Sub
Thats All!
Virtual India is a research project by Microsoft Research India, in collaboration with the Government of India's Department of Science and Technology, powered by Microsoft Virtual Earth™ technology.
Check below links for more information on:
With Virtual India one can view maps in Hindi, English, Kannada and Tamil.
In this Sample code we will see how to customize calendar control to limit the user to select the day on or after today
Step 1: Drag Drop Calendar Control
<asp:Calendar id="Calendar1" runat="server"></asp:Calendar>
Step 2: In code behind use the below code in DayRender Event
The DayRenderEventArgs object contains two properties that you can use to programmatically control the format of the date cell. The Cell property represents the cell being rendered, while the Day property represents the date to render in the cell.
Set the Day's IsSelectable property to true or false depending on the criteria. In this code sample we will limit user to select day on or after today.
VB.NET Code
Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
e.Day.IsSelectable = e.Day.Date >= DateTime.Now
End Sub
C# Code
void
OnDayRender (Object sender, DayRenderEventArgs e) {
e.Day.IsSelectable = e.Day.Date >= DateTime.Now;
}