How to Handle Null Values in Gridview

Leave a Comment
In this tutorial, we have learn how to handle NULL values when binding gridview in ASP.NET

For handling Null values in Gridview Create RowDataBound event for Gridview, then first check the row type by using following sentence.

if (e.Row.RowType == DataControlRowType.DataRow)
{
           //add logic
}
Now, add the logic between two curly brackets. For Example if we want to handle Null Values.
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var couseName = e.Row.FindControl("lbl_studentCourse") as Label;
            if (couseName.Text == "" || couseName.Text == null)
            {
                couseName.Text = "Not Applicable for Any Course";
                couseName.ForeColor = Color.Blue;
            }
        } 
So the complete Source code for this demo as Follow :

Default.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="mGrid"
            PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
                    onrowcreated="GridView1_RowCreated">
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lbl_studentName" runat="server" Text='<%# Bind("Student_name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Roll No">
                    <ItemTemplate>
                        <asp:Label ID="lbl_StudentRollNo" runat="server" Text='<%# Bind("Student_rollno") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Couse">
                    <ItemTemplate>
                        <asp:Label ID="lbl_studentCourse" runat="server" Text='<%# Bind("student_course") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>            
            </Columns>
        </asp:GridView>

Default.aspx.cs

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Student"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadStudentData();
        }
    }
    private void LoadStudentData()
    {
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Student", con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //first check the row type
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var couseName = e.Row.FindControl("lbl_studentCourse") as Label;
            if (couseName.Text == "" || couseName.Text == null)
            {
                couseName.Text = "Not Applicable for Any Course";
                couseName.ForeColor = Color.Blue;
            }
        }
    }


0 comments:

Post a Comment