using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Grid : System.Web.UI.Page,ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string _fields;
SqlConnection sqlConnection;
SqlCommand sqlCommand;
SqlDataAdapter dataAdapter;
DataSet dataSet;
DataTable _dtReport;
SqlDataAdapter sqlDataAdapter;
DataTable dtReport;
DataView dataView;
//GridView dynamicgrid;
TemplateField templateField;
string firstcoulmn, secondcolumn;
public Grid()
{ }
public Grid(DataControlRowType type, string colname,string
fileds,DataTable dtReport)
{
templateType = type;
columnName = colname;
_fields = fileds;
_dtReport = dtReport;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal literalColumn = new Literal();
literalColumn.Text = "<b>" + columnName + " : </b>";
container.Controls.Add(literalColumn);
DropDownList dropdownColumn = new DropDownList();
dropdownColumn.ID = columnName;
dropdownColumn.DataSource = populatedropdown
(columnName,null);
dropdownColumn.DataValueField = columnName;
dropdownColumn.DataTextField = columnName;
//Cache[columnName] = dropdownColumn;
dropdownColumn.Items.Insert(0, new ListItem("ALL",
"ALL"));
dropdownColumn.AutoPostBack = true;
dropdownColumn.SelectedIndexChanged += new EventHandler
(dropdownColumn_SelectedIndexChanged);
container.Controls.Add(dropdownColumn);
break;
case DataControlRowType.DataRow:
Label LabelGridRow = new Label();
LabelGridRow.DataBinding += new EventHandler
(this.LabelGridRow_DataBind);
container.Controls.Add(LabelGridRow);
break;
default:
break;
}
}
public void dropdownColumn_SelectedIndexChanged(object sender,
EventArgs e)
{
DropDownList dl = (DropDownList)sender;
string[] objfileds = _fields.Split(',');
int fieldcount = objfileds.Length;
string filterquery = "1=1";
if (dl.ID == objfileds[0])
{
firstcoulmn =dl.SelectedValue;
DropDownList SecondDropDown = (DropDownList)
(GridView1.HeaderRow.FindControl(objfileds[1]));
if(SecondDropDown.SelectedValue != "ALL")
{
if(firstcoulmn != "ALL")
filterquery = dl.ID + "='" + dl.SelectedValue + "'
AND" + SecondDropDown.ID + "='" + SecondDropDown.SelectedValue + "'";
else
filterquery = SecondDropDown.ID + "='" +
SecondDropDown.SelectedValue + "'";
}
else
{
if(firstcoulmn != "ALL")
filterquery = dl.ID + "='" + dl.SelectedValue +
"'" ;
}
}
else if(dl.ID == objfileds[1])
{
secondcolumn = dl.SelectedValue;
DropDownList FirstDropDown = (DropDownList)
(GridView1.HeaderRow.FindControl(objfileds[0]));
if(FirstDropDown.SelectedValue != "ALL")
{
if(secondcolumn != "ALL")
filterquery = dl.ID + "='" + dl.SelectedValue + "'
AND" + FirstDropDown.ID + "='" + FirstDropDown.SelectedValue + "'";
else
filterquery = FirstDropDown.ID + "='" +
FirstDropDown.SelectedValue + "'";
}
else
{
if(secondcolumn != "ALL")
filterquery = dl.ID + "='" + dl.SelectedValue +
"'" ;
}
}
dataView = _dtReport.DefaultView;
dataView.RowFilter = filterquery;
GridView1.DataSource = dataView;
GridView1.DataBind();
GridView1.Visible = true;
}
private void LabelGridRow_DataBind(Object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text = DataBinder.Eval(row.DataItem, columnName).ToString
();
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dtrows;
string selectfields = "city,state";
if (!IsPostBack)
{
BindGrid(selectfields);
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
//DataTable dtrows;
//string selectfields = "city,state";
////if (!IsPostBack)
////{
// BindGrid(selectfields);
//}
// else
// {
//dtrows = (DataTable)(DataTable)Cache["dtReport"];
////GridView1.Columns.Add((TemplateField)Cache
["templatefields"]);
//BindTemplateFields(selectfields,dtrows);
//GridView1.DataSource = dtrows;
//GridView1.DataBind();
//DropDownList dropdowncitycolumn = (DropDownList)
(GridView1.HeaderRow.FindControl("city"));
//DropDownList dropdownstatecolumn = (DropDownList)
(GridView1.HeaderRow.FindControl("state"));
// dropdowncitycolumn.SelectedValue = firstcoulmn;
// dropdownstatecolumn.SelectedValue = secondcolumn;
// }
}
public DataSet populatedropdown(string column, string
dropdownfilter)
{
string query = "";
query = "select distinct(" + column + ") from authors";
if (dropdownfilter == null)
query = "select distinct(" + column + ") from authors";
else
query = query + dropdownfilter;
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
ConfigurationManager.AppSettings["ConnectionString"].ToString();
sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = query;
dataAdapter = new SqlDataAdapter(sqlCommand);
dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
public void BindGrid(string fields)
{
string selectquery = "";
sqlConnection = new SqlConnection();
sqlCommand = new SqlCommand();
sqlConnection.ConnectionString =
ConfigurationManager.AppSettings["ConnectionString"].ToString();
sqlConnection.Open();
sqlCommand.Connection = sqlConnection;
selectquery = "select ";
selectquery = selectquery + fields + " from authors";
sqlCommand.CommandText = selectquery;
sqlDataAdapter = new SqlDataAdapter(sqlCommand);
dtReport = new DataTable();
sqlDataAdapter.Fill(dtReport);
//Bind Template Fields
BindTemplateFields(fields,dtReport);
GridView1.DataSource = dtReport;
GridView1.DataBind();
GridView1.Visible = true;
GridView1.EnableViewState = true;
Cache["dtReport"] = dtReport;
}
public void BindTemplateFields(string fields,DataTable dtReport)
{
for (int i = 0; i < dtReport.Columns.Count; i++)
{
templateField = new TemplateField();
templateField.ItemTemplate = new Grid
(DataControlRowType.DataRow, dtReport.Columns[i].ColumnName, fields,
dtReport);
//Creating Header
templateField.HeaderTemplate = new Grid
(DataControlRowType.Header, dtReport.Columns[i].ColumnName, fields,
dtReport);
//Adding Columns to the grid
// dynamicgrid.Columns.Add(field);
GridView1.Columns.Add(templateField);
}
}
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.Header)
//{
// if (e.Row.Controls.GetType() == Type.GetType
("DropDownList"))
// {
// }
//}
//else
//{
// if (e.Row.RowType == DataControlRowType.DataRow)
// {
// }
//}
}
}
When I select any value from the dropdown, the page is getting
postback and nothing showing in the page.
On Feb 11, 12:23 pm, Brandon Betances <[email protected]> wrote:
> Lets see some code then.