First of all I thank you for early reply. My datasource is SQL, and working on ASP.net: Actually I am working with 3 dropdown lists, when user selects any dropdown list, selected value is transferred to string variable ddlToGrid. Gridview is populated using the ddlToGrid. To avoid unnecessary coding I posted code with one dropdown list and gridview with paging enabled. I have handled OnPaging also. At first instant gridview was populated with selected ddl value. But while paging i suspect ddl becomes null and gridview showing all records. Seeking your early reply. Thank u in advance
<asp:Content ID="Content5" ContentPlaceHolderID="MainContent" Runat="Server">
<tr>
<td colspan="4">
Choose Details to Search
</td>
</tr>
<tr>
<td colspan="1">
<asp:DropDownList ID="ddlItemType" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="ddlItemType_SelectedIndexChanged"
ToolTip="Select Item Type" >
</asp:DropDownList>
</td>
</asp:DropDownList>
</tr>
<tr>
<td colspan="4">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "False" AllowPaging="true"
OnPageIndexChanging="gdv1_OnPaging" PageSize="5" >
<Columns>
<asp:BoundField DataField="Material" HeaderText="MaterialCode" />
<asp:BoundField DataField="BRDESC" HeaderText="Brief Description" />
<asp:BoundField DataField="Mtrl Grp" HeaderText="Material Gr" />
</Columns>
</asp:GridView>
</td>
</tr>
</asp:Content>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Materialstatus : System.Web.UI.Page
{
String ddlToGrid = string.Empty;
String GridViewString = string.Empty;
SqlConnection con = new SqlConnection(("Data Source=xxxxxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xx; password=xxx"));
SqlDataReader ddlloadingReader = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlloading();
}
}
protected void ddlloading()
{
ddlItemType.AppendDataBoundItems = true;
ddlItemType.Items.Add(new ListItem("--Select Item Type--", "-1"));
SqlCommand cmdddl = new SqlCommand();
cmdddl.CommandType = CommandType.Text;
cmdddl.Connection = con;
String strQuery = "select Distinct ITM1 from DATABASE";
cmdddl.CommandText = strQuery;
try
{
con.Open();
ddlloadingReader = cmdddl.ExecuteReader();
ddlItemType.DataSource = ddlloadingReader;
ddlItemType.DataTextField = "ITM1";
ddlItemType.DataBind();
cmdddl.Parameters.Clear();
ddlloadingReader.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmdddl.Dispose();
}
}
protected void ddlItemType_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewString = " ITM1 = ";
ddlToGrid = ddlItemType.SelectedItem.Value;
this.BindGrid(ddlToGrid);
}
protected void BindGrid(String ddlToGrid)
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
String strQuery = "select Material,BRDESC,[Mtrl Grp] from DATABASE where" +
GridViewString + "@ITM1" + " Order By Material";
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@ITM1", ddlToGrid);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
da.SelectCommand = cmd;
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
protected void gdv1_OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid(ddlToGrid);
}