You can control the appearance of the DataGrid control by programmatically adding attributes to the and tags rendered by the control on the browser. Attributes can be programmatically added by providing code in the event handler for the OnItemCreated or OnItemDataBound event.
To add an attribute to the tag, first get the TableCell object that represents the cell in the DataGrid control you want to add the attribute to. TheControl.Controls collection for the Item property of the DataGridItemEventArgs object passed into the event handler can be used to get the desiredTableCell object. You can then use the AttributeCollection.Add method of the Attributes collection for the TableCell object to add attributes to the tag.
Public partial class _Default :System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
SqlConnection con = new
SqlConnection(ConfigurationManager.AppSettings["connect"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from
Prices";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
dgdGlobalPrice.DataSource = ds;
dgdGlobalPrice.DataBind();
con.Close();
}
protected void dgdGlobalPrice_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dgdGlobalPrice.CurrentPageIndex =
e.NewPageIndex;
BindData();
}
protected void dgdGlobalPrice_ItemDataBound(object sender, DataGridItemEventArgs e)
{
// Use
the ItemDataBound event to customize the DataGrid
control.
// The
ItemDataBound event allows you to access the data
before
// the
item is displayed in the control. In this example,
the
//
ItemDataBound event is used to format the items in the
//
CurrencyColumn in currency format.
if ((e.Item.ItemType ==
ListItemType.Item)||(e.Item.ItemType
== ListItemType.AlternatingItem))
{
// Retrieve
the text of the
CurrencyColumn from the DataGridItem
// and convert the
value to a Double.
Double Price = Convert.ToDouble(e.Item.Cells[2].Text);
// Format the value
as currency and redisplay
it in the DataGrid.
e.Item.Cells[2].Text =
Price.ToString("c");
}
}
protected void dgdGlobalPrice_ItemCreated(object sender, DataGridItemEventArgs e)
{
Label1.Text = Label1.Text + " "+ e.Item.ItemIndex;
}
protected void dgdGlobalPrice_ItemCommand(object source, DataGridCommandEventArgs e)
{
switch (((LinkButton)e.CommandSource).CommandName)
{
case "Delete":
//Write Delete Function here
break;
// Add other cases here, if there are
multiple
ButtonColumns in
// the DataGrid control.
default:
// Do nothing.
break;
}
}
protected void dgdGlobalPrice_EditCommand(object source, DataGridCommandEventArgs e)
{
dgdGlobalPrice.EditItemIndex =
e.Item.ItemIndex;
BindData();
}
protected void
dgdGlobalPrice_DeleteCommand(object source, DataGridCommandEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connect"]);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
int EmpId = (int)dgdGlobalPrice.DataKeys[(int)e.Item.ItemIndex];
cmd.CommandText = "Delete from
Employee where EmpId=" + EmpId;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
dgdGlobalPrice.EditItemIndex = -1;
BindData();
}
}
No comments:
Post a Comment