how to display the data in listview using scenario like choose type1 then all the type 1 item shows and if you have choose type2 then all type 2..
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Upendra Pratap ShahiPosted Jul 14, 2015, 6:10 AM
Design Page-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListViewGroup.aspx.cs" Inherits="ListViewGroup" %>
DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
<h3>ListViewh3>
<asp:DropDownList ID="ddlGroup" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged">
<asp:ListItem>Cat1asp:ListItem>
<asp:ListItem>Cat2asp:ListItem>
asp:DropDownList>
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<table border="1">
<tr>
<th>Product
th>
<th>Quantity
th>
<th>Price
th>
<th>Category
th>
tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1">asp:PlaceHolder>
<tr>
<td colspan="3">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
Fields>
asp:DataPager>
td>
tr>
table>
LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1">asp:PlaceHolder>
tr>
GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("Product_Name") %>
td>
<td>
<%# Eval("Quantity") %>
td>
<td>
<%# Eval("Price") %>
td>
<td>
<%# Eval("Category") %>
td>
ItemTemplate>
asp:ListView>
div>
form>
body>
html>
Code Behind page-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ListViewGroup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string val = ddlGroup.SelectedItem.Text.ToString();
if (val != "")
{
BindListView(val);
}
}
}
protected void BindListView(string sGroup)
{
//Creating a dataset
DataSet ds = new DataSet();
DataTable dt;
DataRow dr;
DataColumn pName;
DataColumn pQty;
DataColumn pPrice;
DataColumn pCategory;
//create an object of datatable
dt = new DataTable();
//creating column of datatable with datatype
pName = new DataColumn("Product_Name", Type.GetType("System.String"));
pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
pCategory = new DataColumn("Category", Type.GetType("System.String"));
//bind data table columns in datatable
dt.Columns.Add(pName);
dt.Columns.Add(pQty);
dt.Columns.Add(pPrice);
dt.Columns.Add(pCategory);
//creating data row and assiging the value to columns of datatable
dr = dt.NewRow();
dr["Product_Name"] = "Product 1";
dr["Quantity"] = 2;
dr["Price"] = 200;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 2";
dr["Quantity"] = 5;
dr["Price"] = 480;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 3";
dr["Quantity"] = 8;
dr["Price"] = 100;
dr["Category"] = "Cat1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Product_Name"] = "Product 4";
dr["Quantity"] = 2;
dr["Price"] = 500;
dr["Category"] = "Cat2";
dt.Rows.Add(dr);
//Add datatable to the dataset
ds.Tables.Add(dt);
var dv = ds.Tables[0].DefaultView;
dv.RowFilter = "Category='" + sGroup + "'";
DataSet ds1 = new DataSet();
var newdt = dv.ToTable();
ds1.Tables.Add(newdt);
//bind data to data controls
lvCustomers.DataSource = ds1.Tables[0];
lvCustomers.DataBind();
}
//paging code
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.BindListView(ddlGroup.SelectedItem.Text.ToString());
}
protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{
string val1 = ddlGroup.SelectedItem.Text.ToString();
if (val1 != "")
{
BindListView(val1);
}
}
}
Upendra Pratap ShahiPosted Jul 14, 2015, 7:22 AM
anamika garewalPosted Jul 14, 2015, 6:26 AM