Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
but it not work
i store data in viestaete
and i use dropdownlist to filter record
now my problem is when dropdown selecting index is zero then show all record other wise show only selecting record

What I have tried:

<pre> DataTable dt = (DataTable)ViewState["dtQueAns"];
        DataView dv = new DataView(dt);
        dv.RowFilter = "if Date =0 then Date else Date ='" + Date.ToString("dd/MM/yyyy") + "'";
        dt = dv.ToTable();
        rptEvantlog.DataSource = dt;
        rptEvantlog.DataBind();
Posted
Updated 17-Jun-19 23:52pm

Row filters can't involve "external values": they have no access to the variables in your program. So while you can include the current value of the selection in your filter, if it changes, that won't affect the display unless you change the filter string.

Start by using the view directly:
private DataView dv = null;
...
dv = new DataView(dt);
...
rptEvantlog.DataSource = dv;
You can then set the RowFilter in the SelectionChanged event for your dropDown:
dv.RowFilter = $"{index} = 0 OR Date = #{matchDate}#";
 
Share this answer
 
Please, read the documentation, especially Remarks: DataView.RowFilter Property (System.Data) | Microsoft Docs[^]

RowFilter accepts an expression, so, it has to contain a column name and a value, for example:
C#
dv.RowFilter = "DateOfTransaction=#{ProperDate}#";

In other words, you can built your own expression this way:
C#
string s = DropDown1.SelectedIndex==0 ? DateTime.Today.ToString("d") : DropDown1.Value.ToString("d");
dv.RowFilter = string.Format("DateColumn=#{0}#", s); 



For further details, please see:
DataColumn.Expression Property (System.Data) | Microsoft Docs[^]
DateTime.Date Property (System) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900