Tuesday, 7 June 2016

Bind Empty gridview using dataset or datatable with no record found message

Design Page ie ASPX Page


 <asp:GridView ID="gvnews" runat="server"
                AutoGenerateColumns="False" PageSize="10" Width="100%">
                <Columns>
                    <asp:TemplateField HeaderText="S.NO" ControlStyle-Width="50px">
                        <ItemTemplate>
                            <%# Container.DataItemIndex + 1 %>
                        </ItemTemplate>
                        <ControlStyle Width="50px" />
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="Property Name" DataField="PropertyName" />
                    <asp:BoundField HeaderText="City" DataField="City" /> 
                </Columns>               
                <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="First"
                    LastPageText="Last" />            
 </asp:GridView>

 Code Behind ie ASPX.cs Page


 //define connection 
SqlConnection con= "your Connection";
con.Open();
//define your query;
string query="";
  SqlCommand cmd = new SqlCommand("query", con);
            DataTable dt = new DataTable();
            DataSet ds= new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
sda.fill(ds);
            con.Close();
//using data set
public void bind_GridviewUsingDataSet()
{
       gvnews.DataSource = ds.Tables[0];
            if (ds.Tables[0].Rows.Count >0)
            {
                 gvnews.DataSource = ds;
                gvnews.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                gvnews.DataSource = ds;
                gvnews.DataBind();
                int columncount = gvnews.Rows[0].Cells.Count;
                gvnews.Rows[0].Cells.Clear();
                gvnews.Rows[0].Cells.Add(new TableCell());
                gvnews.Rows[0].Cells[0].ColumnSpan = columncount;
                gvnews.Rows[0].Cells[0].Text = "No Records Found";
                gvnews.Rows[0].Cells[0].ForeColor = Color.Red;            
            }

}

using DataTable
public void bind_GridviewUsingDataSet()
{

 GridView1.DataSource = dt;
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                GridView1.DataSource = dt;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
                GridView1.Rows[0].Cells[0].ForeColor = Color.Red;
            }

}

This is my first post on this blog, if you find any mistake please let me know.

No comments:

Post a Comment