54118

How to make DropDownList dynamic from filtered GridView

Question:

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/JguWL.png" data-original="https://i.stack.imgur.com/JguWL.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Here is my Asp.net code:

<asp:UpdatePanel runat="server" ClientIDMode="Static" ID="TasksUpdatePanel" UpdateMode="Conditional"> <ContentTemplate> <table class="taskGridView"> <tr> <td> <asp:DropDownList ID="ddlTaskName" CssClass="chosen-select" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlTaskName_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2739] 'Task Name' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlService" CssClass="chosen-select" DataSourceID="dsPopulateService" AutoPostBack="true" DataValueField="Service" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlService_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateService" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2846] 'Service' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlStatus" CssClass="chosen-select" DataSourceID="dsPopulateStatus" AutoPostBack="true" DataValueField="Status" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlStatus_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateStatus" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2812] 'Status' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlDueDate" CssClass="chosen-select" DataSourceID="dsPopulateDueDate" AutoPostBack="true" DataValueField="Due Date" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlDueDate_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateDueDate" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT CONVERT(VARCHAR(14), [ATTR2752], 110) 'Due Date' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlOwner" CssClass="chosen-select" DataSourceID="dsPopulateOwner" AutoPostBack="true" DataValueField="Owner" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlOwner_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateOwner" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [REALNAME] 'Owner' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlClient" CssClass="chosen-select" DataSourceID="dsPopulateClient" AutoPostBack="true" DataValueField="Client" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlClient_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateClient" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2815] 'Client' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlSite" CssClass="chosen-select" DataSourceID="dsPopulateSite" AutoPostBack="true" DataValueField="Site" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlSite_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateSite" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2819] 'Site' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlPractice" CssClass="chosen-select" DataSourceID="dsPopulatePractice" AutoPostBack="true" DataValueField="Practice" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlPractice_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulatePractice" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2817] 'Practice' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> <td> <asp:DropDownList ID="ddlProvider" CssClass="chosen-select" DataSourceID="dsPopulateProvider" AutoPostBack="true" DataValueField="Provider" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlProvider_onSelectIndexChanged"> <asp:ListItem Text="All" Value="%"></asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="dsPopulateProvider" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="SELECT DISTINCT [ATTR2919] 'Provider' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID WHERE CT.ACTIVESTATUS = 0"></asp:SqlDataSource> </td> </tr> </table> <asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated"> <Columns> <asp:HyperLinkField Target="_blank" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Site" HeaderText="Site" SortExpression="Site" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Practice" HeaderText="Practice" SortExpression="Practice" ItemStyle-CssClass="taskTableColumn" /> <asp:BoundField DataField="Provider" HeaderText="Provider" SortExpression="Provider" ItemStyle-CssClass="taskTableColumn" /> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel>

My code-behind:

protected void Page_Load(object sender, EventArgs e) { strMainQuery = @"SELECT CT.OBJECTID, 'http://checkthis.com/d=' + CAST(CT.OBJECTID AS VARCHAR) + '&classid=1224' 'Task Detail' ,LTRIM(RTRIM(CT.ATTR2846)) 'Service' ,LTRIM(RTRIM(CT.ATTR2812)) 'Status' ,CONVERT(VARCHAR(14), CT.ATTR2752, 110) 'Due Date' ,LTRIM(RTRIM(CT.ATTR2739)) 'Task Name' ,LTRIM(RTRIM(UA.REALNAME)) 'Owner' ,LTRIM(RTRIM(C.ATTR2815)) 'Client' ,RTRIM(SI.ATTR2819) 'Site' ,RTRIM(PR.ATTR2817) 'Practice' ,RTRIM(P.ATTR2919) 'Provider' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID"; if (!Page.IsPostBack) { ViewState["sortOrder"] = "Asc"; ViewState["sortExp"] = "Due Date"; PullData("Due Date", "Asc"); //ASC: A (top) to Z (bottom) || # (low to high) || Date (oldest to newest) } } public void PullData(string sortExp, string sortDir) { query = ""; DataTable taskData = new DataTable(); connString = @""; //connectionstring if (ddlTaskName.SelectedIndex > 0) { strClause += " AND CT.ATTR2739 = '" + ddlTaskName.SelectedItem.Text + "'"; } else { strClause += " AND CT.ATTR2739 LIKE '%'"; } if (ddlService.SelectedIndex > 0) { strClause += " AND CT.ATTR2846 = '" + ddlService.SelectedItem.Text + "'"; } else { strClause += " AND CT.ATTR2846 LIKE '%'"; } if (ddlStatus.SelectedIndex > 0) { strClause += " AND CT.ATTR2812 = '" + ddlStatus.SelectedItem.Text + "'"; } else { strClause += " AND CT.ATTR2812 LIKE '%'"; } if (ddlDueDate.SelectedIndex > 0) { strClause += " AND CONVERT(VARCHAR(14), CT.ATTR2752, 110) = '" + ddlDueDate.SelectedItem.Text + "'"; } else { strClause += " AND CONVERT(VARCHAR(14), CT.ATTR2752, 110) LIKE '%'"; } if (ddlOwner.SelectedIndex > 0) { strClause += " AND UA.REALNAME = '" + ddlOwner.SelectedItem.Text + "'"; } else { strClause += " AND UA.REALNAME LIKE '%'"; } if (ddlClient.SelectedIndex > 0) { strClause += " AND C.ATTR2815 = '" + ddlClient.SelectedItem.Text + "'"; } else { strClause += " AND C.ATTR2815 LIKE '%'"; } if (ddlSite.SelectedIndex > 0) { strClause += " AND SI.ATTR2819 = '" + ddlSite.SelectedItem.Text + "'"; } else { strClause += " AND SI.ATTR2819 LIKE '%'"; } if (ddlPractice.SelectedIndex > 0) { strClause += " AND PR.ATTR2817 = '" + ddlPractice.SelectedItem.Text + "'"; } else { strClause += " AND PR.ATTR2817 LIKE '%'"; } if (ddlProvider.SelectedIndex > 0) { strClause += " AND P.ATTR2919 = '" + ddlProvider.SelectedItem.Text + "'"; } else { strClause += " AND P.ATTR2919 LIKE '%'"; } if (ddlTaskName.SelectedIndex == 0 && ddlService.SelectedIndex == 0 && ddlStatus.SelectedIndex == 0 && ddlDueDate.SelectedIndex == 0 && ddlOwner.SelectedIndex == 0 && ddlClient.SelectedIndex == 0 && ddlSite.SelectedIndex == 0 && ddlPractice.SelectedIndex == 0 && ddlProvider.SelectedIndex == 0) { query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0"; } else { query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0" + strClause; } using (SqlConnection conn = new SqlConnection(connString)) { try { SqlCommand cmd = new SqlCommand(query, conn); // create data adapter SqlDataAdapter da = new SqlDataAdapter(query, conn); // this will query your database and return the result to your datatable DataSet myDataSet = new DataSet(); da.Fill(myDataSet); myDataView = new DataView(); myDataView = myDataSet.Tables[0].DefaultView; if (sortExp != string.Empty) { //MessageBox.Show(sortExp); //MessageBox.Show(sortDir); myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir); } yourTasksGV.DataSource = myDataView; yourTasksGV.DataBind(); TasksUpdatePanel.Update(); conn.Close(); } catch (Exception ex) { string error = ex.Message; } } } protected void ddlTaskName_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlOwner_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlService_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlStatus_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlDueDate_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlClient_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlSite_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlPractice_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } protected void ddlProvider_onSelectIndexChanged(object sender, EventArgs e) { PullData(ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString()); } public string sortOrder { get { if (ViewState["sortOrder"].ToString() == "Desc") { ViewState["sortOrder"] = "Asc"; } else { ViewState["sortOrder"] = "Desc"; } return ViewState["sortOrder"].ToString(); } set { ViewState["sortOrder"] = value; } }

How can I achieve the Dynamic DropDownList from the filtered GridView?

Thanks in advance.

Answer1:

one thing you could try

<ol><li>sore all drop down queries in a private var excluding the where clause</li> <li>you could create a method that will build the where clause for all (drop down list and gridview)</li> <li>when data binding the grid view also rebind the drop down.</li> </ol>

<em>NOTE</em>: being that you are using append data bound items you would need to clear out the current options. An alternative to that would be something like

select 'All' as [Task Name], '%' as value union SELECT DISTINCT [ATTR2739] as 'Task Name', [ATTR2739] as 'Value' FROM .....

if you set you private select vars up like

var _selectForADropDown = "select distinct ...... {0}"

then where you apply the filter to the gridview you can also apply the filter to the data source for the drop down

dataSourceForDropDown.SelectStatement = string.Format(selectForADropDown, MethodForTheWareClasues()); dropDownList.DataBind();

one thing that may help

using private const vars for each part of the query so you can reuse it

break up the query so the where and from clause can be used for both the gridview and the drop downs

private const string StrMainQuery = @"SELECT CT.OBJECTID, 'http://checkthis.com/d=' + CAST(CT.OBJECTID AS VARCHAR) + '&classid=1224' 'Task Detail' ,LTRIM(RTRIM(CT.ATTR2846)) 'Service' ,LTRIM(RTRIM(CT.ATTR2812)) 'Status' ,CONVERT(VARCHAR(14), CT.ATTR2752, 110) 'Due Date' ,LTRIM(RTRIM(CT.ATTR2739)) 'Task Name' ,LTRIM(RTRIM(UA.REALNAME)) 'Owner' ,LTRIM(RTRIM(C.ATTR2815)) 'Client' ,RTRIM(SI.ATTR2819) 'Site' ,RTRIM(PR.ATTR2817) 'Practice' ,RTRIM(P.ATTR2919) 'Provider'"; private const string _from = @" FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) LEFT JOIN HSI.RMOBJECTINSTANCE1251 PXC ON PXC.FK2924 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1249 P ON PXC.FK2923 = P.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1248 PRXC ON PRXC.FK2912 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1230 PR ON PR.OBJECTID = PRXC.FK2911 LEFT JOIN HSI.RMOBJECTINSTANCE1247 SXC ON SXC.FK2908 = CT.OBJECTID LEFT JOIN HSI.RMOBJECTINSTANCE1229 SI ON SI.OBJECTID = SXC.FK2907 INNER JOIN HSI.RMOBJECTINSTANCE1231 C ON CT.FK2927 = C.OBJECTID "; private const string where = @" WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2739 LIKE '{0}' AND CT.ATTR2846 LIKE '{1}' AND CT.ATTR2812 LIKE '{2}' AND AND CONVERT(VARCHAR(14), CT.ATTR2752, 110) LIKE '{3}' AND UA.REALNAME LIKE '{4}' AND C.ATTR2815 LIKE '{5}' AND SI.ATTR2819 LIKE '{6}' AND PR.ATTR2817 LIKE '{7}' AND P.ATTR2919 LIKE '{8}' ";

then build your query's something like:

private string GetWhere() { return string.Format(where, ddlTaskName.SelectedValue, ddlService.SelectedValue, ddlStatus.SelectedValue, ddlDueDate.SelectedValue, ddlOwner.SelectedValue, ddlClient.SelectedValue, ddlSite.SelectedValue, ddlPractice.SelectedValue, ddlProvider.SelectedValue ); } private string GetGridSelect() { return string.Concat(StrMainQuery, _from, GetWhere()); }

then you could build the query for each drop down usingthe union all method above to keep the "all"

private void ReloadDropDown() { dsPopulateProvider.SelectCommand = GetDropDownGuery(); ddlProvider.DataBind(); }

you would benefit a lot by creating a view of this select then using entity framework. would make it really simple to get the value in one swoop for all item but my answer is based off what you currently are doing.

The steps I would take to make these changes

<ol><li>add the const vars for the select statement above the on load method</li> <li>add method to get the where clause (example above)</li> <li>add method to get complete select statement for the grid view(sample above)</li> <li>pull all code building the where clause in the current data bind and method for the grid view and get the select statement from the method just added</li> <li>add method for building the distinct select statements for each drop down and rebind them as shown above </li> </ol>

let me know if you have any question, hope this helps

Recommend

  • Showing rows 0 - 9 ( 10 total, Query took 0.0002 sec) [LatestMoodTime: [BLOB - 10B] - [BLOB - 0B]]
  • Select with condition
  • Python Issues with a Class
  • “socket.gaierror: [Errno 11004] getaddrinfo failed”
  • Windows/C# system-level sequential number generator?
  • Django Rest Framework: getting lists from query_params without request.getlist
  • Deploy same Javascript webapp build to different environments
  • Cannot send user message with Spring Websocket
  • Declare PtrSafe Compatibility in Excel
  • How to add plugin dependencies to pom.xml
  • PostgreSQL 9.1 timezones
  • OAuth2 flow for mobile app
  • How to update docker-compose on Docker for Windows?
  • How to reply a tweet using the Twitter gem?
  • iPhone dealing with xml vs soap vs JSON vs RESTful
  • PHP websocket on SSL with proxy_wsTunnel - Apache
  • Adding Client Certifcate to Service Fabric
  • Handling exceptions in a class library enveloping a device driver
  • How to stop a goroutine that is listening for RethinkDB changefeeds?
  • Is the Go HTTP handler goroutine expected to exit immediately in this case?
  • Android gcm notification payload vs data payload?
  • Type Native Wifi.Wlan + WlanReasonCode cannot be marshaled error
  • How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configur
  • Telerik Radgrid GridDataItem.DataItem is empty when updating (OnUpdateCommand handler)
  • Spring Boot OAuth2 SSO with access/refresh tokens is not stored in a database correctly
  • Multiple sockets for clients to connect to
  • Catch 500 internal server error with System.WebClient
  • oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Invalid JWT
  • Using an enum contained in a Cloud Endpoint model on a Android client
  • GWT Widget.addHandler
  • Python: sending key press events over SSH
  • Error in making a socket connection
  • What is the difference between a “service account” and an “installed application”?
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Python pandas melting data to multiple columns and coulmn names in another column
  • what makes a request a new request in asp.net C#
  • Spring Cloud Microservice Architecture Confusion
  • Transactional Create with Validation in ServiceStack Redis Client
  • Handling un-mapped Rest path
  • PHP - How to update data to MySQL when click a radio button