Friday, March 12, 2010

use of datagrid , data table and dataset

public void GetAllCarrer()
{
// GET DATA DEPENDING UPON USER SELECTION
try
{

conn = new SqlConnection(ConfigurationManager.AppSettings["connsql"]);
string strQuery = "select * from tbl_name ORDER BY id DESC";
SqlCommand cmd = new SqlCommand(strQuery, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "tbl_name");
DataTable dataTable = ds.Tables[0];
//datagrid name
dgDatalist.DataSource = ds;
dgDatalist.DataBind();
ViewState["Data"] = ds;
if (dgDatalist.Items.Count == 0)
{
dgDatalist.Visible = false;
lblrec.Text = "There Is no Any Server Detail.";
}
else
{
dgDatalist.Visible = true;

}
}
catch (Exception err)
{
Response.Write(err.Message);
}
}


user define Design in datagrid function

public void dgDatalist_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
try
{
// Get the newly created item
ListItemType itemType = e.Item.ItemType;

// Is it the pager?
if (itemType == ListItemType.Pager)
{
// Extract the Pager
TableCell pager = (TableCell)e.Item.Controls[0];

//Add Cell to Row to Hold Row Count Label
TableCell newcell = new TableCell();
newcell.CssClass = "pad-left10 pad5";
newcell.ColumnSpan = 3;
newcell.HorizontalAlign = HorizontalAlign.Left;
newcell.Style["border-color"] = pager.Style["border-color"]; //For a Seamless Look

//Add Label Indicating Row Count
Label lblNumRecords = new Label();
lblNumRecords.ID = "lblNumRecords";
newcell.Controls.Add(lblNumRecords);

//Add Table Cell to Pager
e.Item.Controls.AddAt(0, newcell);

int itest = e.Item.Controls.Count;
//Subtract from Colspan of Original Pager to Account for New Row
pager.ColumnSpan = pager.ColumnSpan;
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}