asp.net listview ItemDataBound


Let’s suppose you have a listview dropdownlist in every row and that there are 20 items in the dropdown list. For input purposes, the user would select the item from the dropdown list and fill in any other fields in the row and click “Add” to add the row. Let’s say that the user added 10 rows on that day and that the user can come back later and view the rows at a later time. So later you would like to retrieve 10 rows from the database and you would like to select the correct item in the dropdown list that matches the item retrieved from the database.

The listview ItemDataBound event allows you to connect an event handler where you can work with data results, and compute and displaying totals.

public void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)

In a Listview the parameter “e” does not go row by row with an index like the GridView. It goes item by item so you need to test to make sure that the current item is a data item. Then, if it is a data item, then you can cast it to an object of type DataRowView. Then you can treat the DataRowView object like if it was a row in a regular GridView control.

First you need to create a ListViewDataItem object by casting from the “e.item” parameter:

ListViewDataItem dataItem = (ListViewDataItem)e.Item;

Then you can use the ListViewDataItem object and cast it to a DataRowView object.

DataRowView lvrow = (DataRowView)dataItem.DataItem;

Now you can fetch the value of the field in the row that you want to compare with all the values in the Dropdown list so that you can select that item in the dropdown list.

String strPartNum = lvrow["Part_Number"].ToString();

Now you need to find the dropdown list control in your listview row using “e.item.findcontrol”:

DropDownList ddlProducts= new DropDownList();

ddlProducts= (DropDownList)e.Item.FindControl("ddlProducts");

Now, instead of looping through each item in the dropdown list and comparing until you find the match, you can just use the FINDBYVALUE or FINDBYTEXT methods in the dropdownlist control. Just pass the value of the item you want to search and assign the result to a ListItem object:

ListItem item = ddlProducts.Items.FindByValue(strPartNum );

Finally you can just pass the ListItem object to the IndxOf method of the dropdown list control and assign the result to the SelectedIndex property:

ddlProducts.SelectedIndex = ddlProducts.Items.IndexOf(item);

UPDATE: Realized that a dropdown does not make sense when the row is not in edit or insert mode. Setting the dropdown to enabled=”false” makes the font too light to read. Therefore, for the ItemTemplate and AlternatingItemTemplate I added a label underneath the dropdown and made the dropdown visible=”false”. Then I added logic to set the label’s Text to the dropdown selecteditem text.

Here is the entire method in the code behind:

public void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)

{

int intProdQty = 0;

decimal decDefPrice = 0.0M;

decimal? decNewPrice = null;

decimal decTotal = 0.0M;

String strPartId = "";

if (e.Item.ItemType == ListViewItemType.DataItem)

{

ListViewDataItem dataItem = (ListViewDataItem)e.Item;

DataRowView lvrow = (DataRowView)dataItem.DataItem;

if (lvrow["Part_Id"] != System.DBNull.Value)

{

strPartId = lvrow["Part_Id"].ToString();

DropDownList ddlProducts = new DropDownList();

ddlProducts = (DropDownList)e.Item.FindControl("ddlProducts");

ListItem itemPartId = ddlProducts.Items.FindByValue(strPartId);

ddlProducts.SelectedIndex = ddlProducts.Items.IndexOf(itemPartId);

Label lblProducts = new Label();

lblProducts = (Label)e.Item.FindControl("lblProducts");

lblProducts.Text = ddlProducts.SelectedItem.Text;

}

if (lvrow["Prodqty"] != System.DBNull.Value)

{

intProdQty = Convert.ToInt32(lvrow["Prodqty"]);

}

if (lvrow["newprice"] != System.DBNull.Value)

{

decNewPrice = Convert.ToDecimal(lvrow["newprice"]);

}

else

{

decNewPrice = null;

}

if (lvrow["defprice"] != System.DBNull.Value)

{

decDefPrice = Convert.ToDecimal(lvrow["defprice"]);

}

TextBox tbTotal = new TextBox();

tbTotal = (TextBox)e.Item.FindControl("txtTotal");

if (decNewPrice == null)

{

decTotal = decDefPrice * intProdQty ;

}

else

{

decTotal = (Convert.ToDecimal(decNewPrice )) * intQty;

}

tbTotal.Text = decTotal.ToString("c");

}

}

No comments:

Post a Comment