In this article, I will depict about few tips and tricks in rowdatabound occasion which is accessible in ASP.NET gridview control and these tips will give the answer for all conceivable necessities when working with rowdatabound occasion in gridview control.

Introduction

Gridview control is most regular control in all asp.net applications to show the information and its capable control and parcel of implicit peculiarities. In this article, i will investigate a few tips and traps in rowdatabound occasion in gridvew control, Actually Rowdatabound occasion will happen when information column is sure to the control in the gridview control.

Discover control in Rowdatabound occasion

Utilizing this occasion we can ready to discover the particular controls, for example, textbox, dropdown, checkbox and get values from control which is utilized 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 ready to do arranging a particular column in a gridview focused around a few conditions, say for instance if value would be all the more then 100 then show, those columns with foundation as tan, fore shade as dark and striking 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

In the event that you need to change the gridview column shade change on mouse over and mouse out then compose the beneath code in rowdatabound occasion.

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

At some point we utilize footer column to include/supplement records, so for this situation in the event that you utilize dropdown list as a part of footer line to include new record then we have to load the information in dropdown list when scrape the information with gridview control and see the beneath code in what manner would we be able to load dropdown list in footer column while rowdatabound occasion happens.

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

By and large we utilize hyperlink to divert other page or open new page to view particular subtle elements focused around a few conditions, beneath case diverting to other page if client is qualified 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

For the most part we set the essential key qualities to the datakeyname property in gridview control and underneath code will help to get that date key qualities connected with a line in rowdatabound occasion.

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 rundown of tips on rowdatabound occasion in gridview control will help you when work with this occasion, a debt of gratitude is in order regarding perusing this article and give your proposals and criticism about this article.

Best ASP.NET Hosting Recommendation

ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers ASP.NET hosting starts from $1/month only. They also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable ASP.NET Hosting, ASPHostPortal.com should be your best choice.