top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use RowCreated event of GridView in ASP.NET?

0 votes
1,927 views
How to use RowCreated event of GridView in ASP.NET?
posted Feb 19, 2016 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

RowCreated event of GridView occurs when each row in a GridView is created and all of its child controls are also created. GridViewRowEventArgs parameter is used to access the properties of created row. First you have to establish which row type is created and then you can change its properties.

1.Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
2.Add a Web Form in the Web Site. No Need to change name of the Page
3.Add a GridView in Web Form

<asp:GridView ID="GridView1" runat="server" onrowcreated="GridView1_RowCreated">
</asp:GridView>

4.Namespaces used in the code

Visual Basic

Imports System.Data
Imports System.Data.SqlClient

Visual C#

using System.Data;
using System.Data.SqlClient;

5.Write below code in Page Load method

I have get data from Products table of NORTHWIND database by setting connection to Server. The DataSet object is filled using SqlDataAdapter. GridView1 is bound to DataSet.

Visual Basic

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim text As String = "SELECT CategoryID, CategoryName FROM Categories"
    Dim connString As String = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True"
    Dim conn As New SqlConnection(connString)
    Dim cmd As New SqlCommand(text, conn)
    conn.Open()
    Dim da As New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    da.Fill(ds)
    conn.Close()

    GridView1.DataSource = ds
    GridView1.DataBind()
End Sub

Visual C#

protected void Page_Load(object sender, EventArgs e)
{
    string text = "SELECT CategoryID, CategoryName FROM Categories";
    string connString = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True";
    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(text, conn);
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    conn.Close();

    GridView1.DataSource = ds;
    GridView1.DataBind();
}

6.Write below code in RowCreated event of GridView1

RowType property is checked to find Header or DataRow. TableCell is created to add another header and its Text property is set. Newly created Header is added to GridView using GridViewRowEventArgs parameter of RowCreated event.

In DataRow, you can change style of rows by setting its properties. I have checked UnitPrice in If condition and set the BackColor property for different unit prices. I have get data this time from Suppliers table of NORTHWIND database by setting connection to Server. A DropDownList is created and data is bound to it. TableCell is created this time for DataRow and DropDownList control is added to it. DataRow is added to GridView by using same GridViewRowEventArgs parameter of RowCreated event.

Visual Basic

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.Header Then
        Dim headerCell As New TableCell()
        headerCell.Text = "Set Supplier"
        headerCell.Font.Bold = True
        e.Row.Cells.Add(headerCell)
    ElseIf e.Row.RowType = DataControlRowType.DataRow Then
        Dim unitPrice As Decimal = CDec(DataBinder.Eval(e.Row.DataItem, "UnitPrice"))

        If unitPrice < 10 Then
            e.Row.BackColor = System.Drawing.Color.Purple
        ElseIf unitPrice = 10 Then
            e.Row.BackColor = System.Drawing.Color.Brown
        Else
            e.Row.BackColor = System.Drawing.Color.Pink
        End If

        Dim text As String = "SELECT SupplierID, CompanyName FROM Suppliers"
        Dim connString As String = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True"
        Dim conn As New SqlConnection(connString)
        Dim cmd As New SqlCommand(text, conn)
        conn.Open()
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        conn.Close()

        Dim DropDownList1 As New DropDownList()

        DropDownList1.DataSource = ds
        DropDownList1.DataTextField = "CompanyName"
        DropDownList1.DataValueField = "SupplierID"
        DropDownList1.DataBind()

        Dim dataCell As New TableCell()
        dataCell.Controls.Add(DropDownList1)
        e.Row.Cells.Add(dataCell)
    End If
End Sub

Visual C#

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        TableCell headerCell = new TableCell();
        headerCell.Text = "Set Supplier";
        headerCell.Font.Bold = true;
        e.Row.Cells.Add(headerCell);
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
        decimal unitPrice = (decimal)DataBinder.Eval(e.Row.DataItem, "UnitPrice");

        if (unitPrice < 10)
        {
            e.Row.BackColor = System.Drawing.Color.Purple;
        }
        else if (unitPrice == 10)
        {
            e.Row.BackColor = System.Drawing.Color.Brown;
        }
        else
        {
            e.Row.BackColor = System.Drawing.Color.Pink;
        }

        string text = "SELECT SupplierID, CompanyName FROM Suppliers";
        string connString = "Data Source=DEV01-PC;Initial Catalog=C:\\SQL SERVER 2000 SAMPLE DATABASES\\NORTHWND.MDF;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(text, conn);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();

        DropDownList DropDownList1 = new DropDownList();

        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "CompanyName";
        DropDownList1.DataValueField = "SupplierID";
        DropDownList1.DataBind();

        TableCell dataCell = new TableCell();
        dataCell.Controls.Add(DropDownList1);
        e.Row.Cells.Add(dataCell);
    }
}

7.Now you can see website in your browser

answer Feb 20, 2016 by Shivaranjini
...