how to bind data into gridview in asp.net
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow pagerRow = gvDetails.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
gvDetails.PageIndex = pageList.SelectedIndex;
}
private void BindGrid()
{
//Set your connection String here
SqlConnection CN = DBUtil.GetCon();
if (CN.State != ConnectionState.Closed)
CN.Close();
CN.Open();
string q = "SELECT Head_Code,Head_Name,Section_Code,Head_Mode,Head_Type,Head_Status FROM eLogs_Acc_Account_Master";
SqlCommand cmd = new SqlCommand(q, CN);
//SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
////Write a select Query
//string q = "select CustomerID,CompanyName,ContactName,Phone,City,Country from Customers";
DataSet dset = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(q, CN);
//cn.Open();
DataTable dtable = new DataTable();
da.Fill(dtable);
//Bind grid.
gvDetails.DataSource = dtable;
gvDetails.DataBind();
CN.Close();
}
protected void gvDetails_DataBound(object sender, EventArgs e)
{
GridViewRow pagerRow = gvDetails.BottomPagerRow;
if (pagerRow == null) return;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
if (pageList == null) return;
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
for (int i = 0; i < gvDetails.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString());
if (i == gvDetails.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
if (pageLabel != null)
{
int currentPage = gvDetails.PageIndex + 1;
}
gvDetails.DataBind();
}
Here i m not able to view data in the grid it displaying empty page