Home > Articles > ASP.NET Articles > RowDatabound event tips and tricks in Gridview control

RowDatabound event tips and tricks in Gridview control

by Thamilselvan Jagadeesan   on Jul 24, 2013   Category: ASP.NET  | Level: Beginner  |  Views: 51808    |  Points: 100   
Like this article? Bookmark and Share:
In this article, I will describe about few tips and tricks in rowdatabound event which is available in asp.net gridview control and these tips will provide the solution for all possible requirements when working with rowdatabound event in gridview control.




Introduction


Gridview control is most common control in all asp.net applications to display the data and it’s very powerful control and lot of built in features. In this article, i will explore some tips and tricks in rowdatabound event in gridvew control, Actually RowDatabound event will occur when data row is bound to the control in the gridview control.

Find control in RowDataBound event


Using this event we can able to find the specific controls such as textbox, dropdown, checkbox and get values from control which is used inside gridview control.

protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {   //Get data row view
            DataRowView drview = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Find dropdown control & get values
                DropDownList dpEmpdept = (DropDownList)e.Row.FindControl("DrpDwnEmpDept");
                string value = dpEmpdept.SelectedItem.Value;
                //Find textbox control
                TextBox txtname = (TextBox)e.Row.FindControl("txtName");
                string Name = txtname.Text;
                //Find checkbox and checked/Unchecked based on values
                CheckBox chkb = (CheckBox)e.Row.FindControl("ChckBxActive");
                if (drview[5].ToString() == "True")
                {
                    chkb.Checked = true;
                }
                else
                {
                    chkb.Checked = false;
                }
            }
        }


 

Format specific row


We can able to do formatting a specific row in a gridview based on some conditions, say for example if price would be more then 100 then display, those rows with background as brown, fore color as black and bold letters.

 protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblPrice = (Label)e.Row.FindControl("lblPrice");
                if (Convert.ToInt32(lblPrice.Text) > 100)
                {
                    e.Row.ForeColor = System.Drawing.Color.Black;
                    e.Row.Font.Bold = true;
                    e.Row.BackColor = System.Drawing.Color.Brown; 
                }
            }
        }

 

Gridview row color change

If you want to change the gridview row color change on mouse over and mouse out then write the below code in rowdatabound event.

protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "self.MouseOverOldColor=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=self.MouseOverOldColor");
            }
        }

 

Bind dropdown list in footer

Sometime we use footer row to add/insert records, so in this case if you use dropdown list in footer row to add new record then we need to load the data in dropdown list when bind the data with gridview control and see the below code how can we load dropdown list in footer row while rowdatabound event occurs.

protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList dp = (DropDownList)e.Row.FindControl("DrpDwnAddEmpDept");
                dp.DataSource = GetEmpDept();
                dp.DataTextField = "DepName";
                dp.DataValueField = "DepName";
                dp.DataBind();
            }
        }
        private DataTable GetEmpDept()
        {
            //Get Employee department
            DataTable dt = new DataTable();
            dt.Columns.Add("DepName");
            DataRow rw1 = dt.NewRow();
            rw1[0] = "IT";
            dt.Rows.Add(rw1);
            DataRow rw2 = dt.NewRow();
            rw2[0] = "Finance";
            dt.Rows.Add(rw2);
            DataRow rw3 = dt.NewRow();
            rw3[0] = "Security";
            dt.Rows.Add(rw3);
            return dt;
        }

 

HyperLink and redirect


In general we use hyperlink to redirect other page or open new page to view specific details based on some conditions, below example redirecting to other page if user is eligible to view.

<asp:GridView ID="GVSample" runat="server" AutoGenerateColumns="false" OnRowDataBound="GVSample_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Userprofile">
                    <ItemTemplate>
                        <asp:HyperLink ID="hyprlnkdetails" runat="server" />
                        <asp:Label ID="lblText" runat="server" Text=""></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView drview = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink HLdetails = e.Row.FindControl("hyprlnkdetails") as HyperLink;
                Label lblText       = e.Row.FindControl("lblText") as Label;
                if (drview[5].ToString()=="True")
                {
                    HLdetails.Text          = "Click here to view";
                    HLdetails.NavigateUrl   = String.Format("~/details.aspx?UserID={0}", drview[8].ToString());
                }
                else
                {
                    HLdetails.Visible   = false;
                    lblText.Text        = "View Restricted";
                }
            }
        }

 

Get DataKeyValues in RowDatabound

Generally we set the primary key values to the datakeyName property in gridview control and below code will help to get that date key values associated with a row in rowdatabound event.

protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                    string userID = GVSample.DataKeys[e.Row.RowIndex].Values[0].ToString();
            }
        }

Above list of tips on rowdatabound event in gridview control will help you when work with this event, thanks for reading this article and provide your suggestions and feedback about this article.
 

User Responses | Post Article

 No Response found!
  Most viewed Articles
 Submit feedback about this article
Please sign in to post feedback