Note that there are some explanatory texts on larger screens.

plurals
  1. POApply Column Filtering in Gridview in asp.net
    primarykey
    data
    text
    <p>I have a gridview which is populated at runtime with a table with unknow number of columns which are not known before hand.</p> <p>I want to apply column filtering on all columns with Drop Down List.</p> <p>How can I achieve it. Using Jquery, Linq or simple C sharp Coding.</p> <pre><code>public partial class DetailView : System.Web.UI.Page { public static int y = 0; public static String colName = ""; public static Boolean flag = false; static String folder = ""; static public MySqlConnection conn = null; static public DropDownList dp = null; protected override void OnInit(EventArgs e) { base.OnInit(e); string strcon = ConfigurationManager.ConnectionStrings["abc"].ConnectionString; conn = new MySqlConnection(strcon); conn.Open(); Response.Write(Session["cols"].ToString()); Response.Write(Session["id"].ToString()); // Response.Write("qw"); folder = Session["folderName"].ToString(); colName = Session["cols"].ToString(); GridBind(folder, colName, flag); } protected void Page_Load(object sender, EventArgs e) { } public void GridBind(String folder, String colName, Boolean val) { GridView1.DataSource = null; GridView1.DataBind(); GridView1.Columns.Clear(); y = 0; MySqlDataAdapter da; if (val == false) { da = new MySqlDataAdapter("select * from " + folder + "", conn); } else { da = new MySqlDataAdapter("select * from " + folder + " where abc= '" + colName + "'", conn); } DataTable dt = new DataTable(); da.Fill(dt); foreach (DataColumn coloumn in dt.Columns) { if (!coloumn.ColumnName.Equals("emp")) { var linkF = new TemplateField(); linkF.HeaderText = coloumn.ColumnName; linkF.HeaderTemplate = new LinkColumn(ListItemType.Header, coloumn.ColumnName,folder); linkF.ItemTemplate = new LinkColumn(ListItemType.Item, coloumn.ColumnName,folder); GridView1.Columns.Add(linkF); } else if (coloumn.ColumnName.Equals("emp")) { //Response.Write("Came"); BoundField bfield = new BoundField(); ////Initalize the DataField value. bfield.DataField = coloumn.ColumnName; ////Initialize the HeaderText field value. bfield.HeaderText = coloumn.ColumnName; GridView1.Columns.Add(bfield); } } GridView1.DataSource = dt; GridView1.DataBind(); } class LinkColumn : DetailView, ITemplate { int id; ListItemType _item; String colN = null; String fold; public LinkColumn(ListItemType item, String colNa, String f) { _item = item; colN = colNa; fold = f; } public void InstantiateIn(System.Web.UI.Control container) { switch (_item) { case ListItemType.Header: DetailView.dp = new DropDownList(); Label lb = new Label(); MySqlCommand cm = new MySqlCommand("select distinct " + colN + " from " + fold + "", conn); MySqlDataAdapter ad = new MySqlDataAdapter(); DataTable d = new DataTable(); ad.SelectCommand = cm; ad.Fill(d); DetailView.dp.DataTextField = colN; DetailView.dp.DataValueField = colN; DetailView.dp.DataSource = d; DetailView.dp.DataBind(); lb.Text = colN.ToUpperInvariant(); dp.AutoPostBack = true; dp.EnableViewState = true; // DetailView.dp.ID = y.ToString(); y++; container.Controls.Add(lb); container.Controls.Add(DetailView.dp); // DetailView.dp.ID = DetailView.y.ToString(); // Response.Write(_Default.dp.ID); DetailView.dp.SelectedIndexChanged += new EventHandler(dp_Selected); break; case ListItemType.Item: TextBox tb1 = new TextBox(); tb1.Enabled = false; tb1.DataBinding += new EventHandler(tb1_Data); tb1.Columns = 30; container.Controls.Add(tb1); break; } } void tb1_Data(object sender, EventArgs e) { TextBox txt = (TextBox)sender; GridViewRow cont = (GridViewRow)txt.NamingContainer; object dataV = DataBinder.Eval(cont.DataItem, colN); if (dataV != DBNull.Value) { txt.Text = dataV.ToString(); } } void dp_Selected(object sender, EventArgs e) { DataTable a = new DataTable(); DropDownList list = (DropDownList)sender; String name = list.SelectedValue; // String ID = list.ID.ToString(); // Session["id"] = ID; Session["cols"] = name; DetailView.colName = Session["cols"].ToString(); DetailView.flag = true; // Response.Write(DetailView.colName); GridBind(fold, DetailView.colName, true); } } } When i am calling GridBind function from event handler it is giving Null pointer Excpetion at GridView1.DataSource = null; </code></pre> <p>My GridView1 is present in DetailView.aspx }</p> <p>Any help be highly appreciated</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload