Frequently Done Task On Datagrid : PART - 2
Tool Tip on Datagrid Cells:
The Following Code will show tool tip on datagrid cells,
protected void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem e.Item.ItemType == ListItemType.Item)
{
e.Item.Cells[1].ToolTip = e.Item.Cells[2].Text;
}
}
Datagrid with DropDownList:
The code below can be used to change the datgrid cell text based on the selection made on the dropdownlist in datagrid.
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem e.Item.ItemType == ListItemType.Item)
{
string[] options = { "Satheesh", "Babu", "Ram" };
DropDownList list = (DropDownList)e.Item.FindControl("ItemDropDown"); list.DataSource = options;
DropDownList list = (DropDownList)e.Item.FindControl("ItemDropDown"); list.DataSource = options;
list.DataBind();
e.Item.Cells[2].Text=list.SelectedItem.Text;
}
}
In HTML View, Find the dropdownlist inside the datagrid tag,
<ItemTemplate>
<asp:DropDownList id="ItemDropDown" OnSelectedIndexChanged="DropDown_SelectedIndexChanged"
Runat="server" AutoPostBack="True"></asp:DropDownList>
</ItemTemplate>
//This event is called whenever the dropdown selected item is changed,dont
// forget to set autopostback to true for the dropdown
protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
TableCell cell = list.Parent as TableCell;
DataGridItem item = cell.Parent as DataGridItem;
int index = item.ItemIndex;
item.Cells[0].Text=list.SelectedItem.Text;
//Setting Cells[0] to selected text
}
Accessing Footer TextBox:
Accessing Footer TextBox:
By using the following code we can access the controls in footer of the datgrid,
private void dgCTF_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex==-1 & e.Item.ItemType==ListItemType.Footer)
{
TextBox t1=(TextBox)e.Item.FindControl("txtFerVal");
t1.Text="Satheesh babu";
}
}
Satheesh



0 Comments:
Post a Comment
<< Home