Insert new item to ListView after first item - c#

I am trying to add a new item in a ListView on itemdatabound. What is the best way to do it?
Data comes from a dataset with
TopicReplyListView.DataSource = TopicReplyDataTable;
TopicReplyListView.DataBind();
on
TopicReply_ItemDataBoundEvent
I want to add text such as "TEST ITEM" and continue to bind
my TopicReply_ItemDataBoundEvent is
protected void TopicReply_ItemDataBoundEvent(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
}
}

Where are you actually trying to add the "Test Item" text?
If you need to add an item to the data source then just do that in the data table before assigning it to the datasource.
If you need to change something in the ItemTemplate then maybe use the ItemCreated event on the list view.

Related

Using mouse click event to get ListViewItem text

I want to use listview to populate it with data and then use mouseclick event to fill some textboxes with data. I looked up an example in msdn:
ListViewItem theClickedOne = listView1.GetItemAt(e.X, e.Y);
ListViewItem theClickedtwo = listView1.FocusedItem;
if (theClickedOne != null)
{
MessageBox.Show(theClickedtwo.ToString());
//do your thing here.
//there is a reference to the listview item we clicked on
//in our theClickedOne variable.
}
but I couldn't think about a way to use it in order to differentiate the listviewitems I use since the fist Column in my program is the same and it will only give me a string with it's name(first Column).I want to have something similar to next example but for treeview.
void treeView1_NodeMouseClick(Object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Node.Text);
}
When populating you ListView, set the Tag property of the items, e.g.
newItem.Tag = "Item 1";
The Tag property has type object, so you can use anything you want here to identify the item. When handling the mouse click event simply check the Tag value again:
if((string)(clickedItem.Tag) == "Item 1")
{
// do stuff for this specific item.
}

Telerik RadGrid- Access Items' GroupFooter

I'm trying to write content to an items' Group Footer on an item's ItemDataBound event.
Anyone have any idea how to access an Item's group footer from the Item itself?
In other words, how would I get an item's associated group footer from the item object as it's being bound?
protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
**GridItemGroupFooter footer = item.GroupFooter** <-- Pseudocode for what I want to do
}
}
you should do it like this
if (e.Item is GridFooterItem)
{
itemtype somenameforitem=(itemtype )grid.FooterRow.FindControl("youritemid");
//for ex if you need textbox then it will be like this
//TextBox texte=(TextBox)gridname.FooterRow.FindControl("textboxid");
}
also you can do it like this.
GridFooterItem footerItem = (GridFooterItem)grid.MasterTableView.GetItems(GridItemType.Footer)[0];
TextBox texte=(TextBox)footerItem.FindControl("textboxid");//accessing Button inside FooterTemplate
I have give the index [0] while getting the grid footer item as the text box is one and only item in my grid footer, if you have multiple footer items, you can give the index of the item you wan to find

get the clicked item from a IsItemClickEnabled ListView in C# and WinRT

how can I get the clicket elevent from a ListView which has the IsItemClickEnabled enabled?
I know how to get the selected Item/Index but not the clicked item.
ItemClick is working but I can not say s.th. like:
Object selection = listView1.SelectedItem;
EDIT:
I have a ListView and I need to catch the clicked item from this list in the following method:
private void listView1_ItemClick(object sender, ItemClickEventArgs e)
{
...
}
I may be missing something, but doesn't the following work for you?
private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as String;
}
Here I assume the items in the list are simple strings, but in general they'll be whatever type you are using in the collection you've bound to the ItemsSource property of the ListView.
I guess you can also try the SelectionChanged event, and get the clicked item as e.AddedItems or MyListView.SelectedItem or MyListView.SelectedItems.

How do I change a ListView dynamically on DataBound?

I have a ListView with a template, it puts a bunch of data in, like X Y Z.
I want to hide show some columns based on criteria, so I have ItemDataBound event, but I don't know how to get the actual listview row so I can do things to do it.
Any ideas?
You can access the ListViewItemEventArgs' Item property to get at the current item (the one being bound to data).
The sample code below (which shows how to customize a ListView item in the ItemDataBound event) was taken from the MSDN documentation:
protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label EmailAddressLabel;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// Display the e-mail address in italics.
EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
EmailAddressLabel.Font.Italic = true;
System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
string currentEmailAddress = rowView["EmailAddress"].ToString();
if (currentEmailAddress == "orlando0#adventure-works.com")
{
EmailAddressLabel.Font.Bold = true;
}
}
}

Change a selected item in a listview based on the selection in another listview

I have two list views. In the Item command event of the first Listview i am showing the second list view in modal popup using ajaxtoolkit.
protected void lvSelection_ItemCommand(object sender, ListViewCommandEventArgs e)
{
this.lvPopup.Visible = true;
this.lvPopup.DataSource = linqdataSource;
this.lvPopup.DataBind();
this.mdlPopup.Show();
}
Now in the itemcommand event of the second list view I need to change the content of the selected item in the first listview.
Is it possible to do that?
protected void lvPopup_ItemCommand(object sender, ListViewCommandEventArgs e)
{
// Set the text of the first list view item to the selected item
// of the second list view.
lstView1.Items[lstView1.SelectedIndex].Text =
lstView2.Items[lstView2.SelectedIndex].Text
}
I'd think that if you were to set the CommandName of the selector button in the first ListView to "Select" - from the second list view's ItemCommand event, you should be able to alter either the SelectedItemTemplate or the current item for the selected item in the first list.
protected void lvPopup_ItemCommand(object sender, ListViewCommandEventArgs e)
{
lvSelection.SelectedItemTemplate = "<div>woohoo!</div>";
// OR...
lvSelection.Items[lvSelection.SelectedIndex].SkinID = "SomeNewSkinForExample";
mdlPopup.Hide();
}
Have you already tried to dynamically generate the items of the List?
On the event code of the 1st list, clear the Items from the 2nd list and populate it with whatever logic suits you.

Categories

Resources