gridview search issue


J

JohnE

Have a gridview that is to have a search available for one column. The
column is called ChangeRequest, with header text of Change Request. Outside
the grid there is a textbox (txtSearch) that the user can type into and the
click a button to search the column. Below is the code for the search button
click. The error I am getting is 'Cannot find column [ChangeRequest].' The
column is there otherwise there wouldn't be any info in it. The sql is the
same one that initially fills the gridview. Just to make sure, I copied the
connection info into the button click to see if that was the cause.

string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].ConnectionString;
string sql = "SEL_GridviewList";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql, connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
//DataView dv = new DataView();

DataTable dt = new DataTable();
DataView dv = new DataView(dt);

string SearchExpression = null;

if (!String.IsNullOrEmpty(txtSearch.Text))
{
SearchExpression = string.Format("{0}'%{1}%'",
GridView1.SortExpression, txtSearch.Text);
}
dv.RowFilter = "ChangeRequest like " + SearchExpression;
GridView1.DataSource = dv;
GridView1.DataBind();

Can anyone see what is missing or what else might be causing the error?

Thanks.
John
 
Ad

Advertisements

J

JohnE

I got it working. I again removed all of the connection info and the
datatable line. I replaced the dataview dv = bindgrid(); which got it
working. The grid even displays the number of pages (10 rows per page)
following the search. But, the searching can have mulitple pages and when
paging is done, the gridview reverts back to its initial load.

How can I prevent the paging from disrupting the search?

Thanks... John
 
J

JohnE

Thanks for the response. I did just that and it came back " ". Which got me
wondering if during the search anything was there to be searched on. Thus,
the connection info being used and then the I changed several items around
and it worked. Still having the paging issue during the search. Same as
what I had in the sorting and paging.
 
J

JohnE

Mark Rae said:
[please don't top-post]
http://www.caliburn.nl/topposting.html
Thanks for the response. I did just that and it came back " ".

As expected...
Still having the paging issue during the search. Same as what I had in
the sorting
and paging.

Store the search string as a ViewState variable - this will mean that it
will survive the paging

I will try to remember to not top post and I can see why.

So, you are saying to take the line;
SearchExpression = string.Format("{0}'%{1}%'", GridView1.SortExpression,
txtSearch.Text);
and put that as a Viewstate? I've tried it several ways (probably both
wrong) and each way got and error at the dv.RowFilter... line. Or were you
referring to another line?
 
J

JohnE

Mark Rae said:
[please don't top-post]
http://www.caliburn.nl/topposting.html
Thanks for the response. I did just that and it came back " ".

As expected...
Still having the paging issue during the search. Same as what I had in
the sorting
and paging.

Store the search string as a ViewState variable - this will mean that it
will survive the paging

Seems like no matter where the viewstate goes, I either get an error or
nothing occurs (at least it isn't an error). I am really a rookie, newbie,
noob, novice (or any other term you would like to use) in trying to figure
all this out. At least the employer knows what my skill set was when they
assigned this to me. Yikes. Can you assist with a nudge in the right
direction?

Here is the whole code;

protected void btnSearch_Click(object sender, EventArgs e)
{
DataView dv = bindgrid();

string SearchExpression = null;

if (!String.IsNullOrEmpty(txtSearch.Text))
{
SearchExpression = string.Format("{0}'%{1}%'",
GridView1.SortExpression, txtSearch.Text);
}
dv.RowFilter = "ChangeRequest like " + SearchExpression;
GridView1.DataSource = dv;
GridView1.DataBind();
}

Thanks.
John
 
J

JohnE

Mark Rae said:
I will try to remember to not top post and I can see why.

Just look at the thread "How to open a file and save it local"! Imagine that
you're joining this newsgroup for the first time and the only post in this
thread available to your newsreader is the very last one...

Seems like no matter where the ViewState goes, I either get an error or
nothing occurs (at least it isn't an error). I am really a rookie,
newbie,
noob, novice (or any other term you would like to use) in trying to figure
all this out.

The term I usually use is "beginner" - everyone has to start somewhere, and
we were all beginners on our first day...

Can you assist with a nudge in the right direction?

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SearchExpression"] = String.Empty;
}
else
{
ViewState["SearchExpression"] = txtSearch.Text;
}
}

protected void btnSearch_Click(object sender, EventArgs e)
{
DataView dv = bindgrid();
string SearchExpression = null;
if (!String.IsNullOrEmpty(ViewState["SearchExpression"]))
{
dv.RowFilter = "ChangeRequest like " +
ViewState["SearchExpression"].ToString() ;
GridView1.DataSource = dv;
GridView1.DataBind();
}
}

Thanks for the reply and the info. Well, it is further along then what it
was.

I added the page_load info. Under the btnSearch, the if (!String.....) is
what is failing now. When I go to run it, the error list reports 2 errors;

1) The best overloaded method match for 'string.IsNullOrEmpty(string)' has
some invalid arguments (which points to the String.
2) Argument '1': cannot convert from 'object' to 'string' (which points to
ViewState)

Any thoughts on these?

Also, the line that was in there SearchExpression = ... that formats the
string did not show above in your nudge. Wouldn't that still be needed in
order to provide the formatting and wild card characters for the search? If
so, would it go under the string SearchExpression = null; line?

Thanks again.
 
Ad

Advertisements

J

JohnE

Mark Rae said:
I added the page_load info. Under the btnSearch, the if (!String.....) is
what is failing now. When I go to run it, the error list reports 2
errors;

1) The best overloaded method match for 'string.IsNullOrEmpty(string)'
has
some invalid arguments (which points to the String.
2) Argument '1': cannot convert from 'object' to 'string' (which points
to
ViewState)

My fault... :-(

if (ViewState["SearchExpression"].ToString() != String.Empty)

Also, the line that was in there SearchExpression = ... that formats the
string did not show above in your nudge. Wouldn't that still be needed in
order to provide the formatting and wild card characters for the search?
If
so, would it go under the string SearchExpression = null; line?

It's a personal preference. Personally, I never use the String.Format
syntax - I was really only pointing out to you where to make the change...

I'd probably use something like:

dv.RowFilter = "ChangeRequest like '" +
ViewState["SearchExpression"].ToString() + "'";

Thanks for the info. It runs but returns nothing. Here is the code sofar:

if (ViewState["SearchExpression"].ToString() != String.Empty)
{
//SearchExpression = string.Format("{0}'%{1}%'",
GridView1.SortExpression, txtSearch.Text);
dv.RowFilter = "ChangeRequest like '" +
ViewState["SearchExpression"].ToString() + "'";
GridView1.DataSource = dv;
GridView1.DataBind();
lblCount.Text = dv.Count.ToString();

With the SearchExpression line in or out it acts the same when it runs. I
type in UL and nothing shows up or zero rows. There is a UL on the first
page so at least one row should appear. In debug mode, it goes all the way
thru with no errors. When hovering over dv.RowFilter it indicates " ". So,
something is not filling the dv.

You know, at times as frustrating as it seems, this is getting interesting
and (to a point) enjoyable. My learning curve is definitely going straight
up. As it should be for a 'beginner'. Who knows, eventually my days working
with Access all these years could be over and replaced with webapps.

Thoughts on the no data fill?
 
J

JohnE

JohnE said:
Mark Rae said:
I added the page_load info. Under the btnSearch, the if (!String.....) is
what is failing now. When I go to run it, the error list reports 2
errors;

1) The best overloaded method match for 'string.IsNullOrEmpty(string)'
has
some invalid arguments (which points to the String.
2) Argument '1': cannot convert from 'object' to 'string' (which points
to
ViewState)

My fault... :-(

if (ViewState["SearchExpression"].ToString() != String.Empty)

Also, the line that was in there SearchExpression = ... that formats the
string did not show above in your nudge. Wouldn't that still be needed in
order to provide the formatting and wild card characters for the search?
If
so, would it go under the string SearchExpression = null; line?

It's a personal preference. Personally, I never use the String.Format
syntax - I was really only pointing out to you where to make the change...

I'd probably use something like:

dv.RowFilter = "ChangeRequest like '" +
ViewState["SearchExpression"].ToString() + "'";

Thanks for the info. It runs but returns nothing. Here is the code sofar:

if (ViewState["SearchExpression"].ToString() != String.Empty)
{
//SearchExpression = string.Format("{0}'%{1}%'",
GridView1.SortExpression, txtSearch.Text);
dv.RowFilter = "ChangeRequest like '" +
ViewState["SearchExpression"].ToString() + "'";
GridView1.DataSource = dv;
GridView1.DataBind();
lblCount.Text = dv.Count.ToString();

With the SearchExpression line in or out it acts the same when it runs. I
type in UL and nothing shows up or zero rows. There is a UL on the first
page so at least one row should appear. In debug mode, it goes all the way
thru with no errors. When hovering over dv.RowFilter it indicates " ". So,
something is not filling the dv.

You know, at times as frustrating as it seems, this is getting interesting
and (to a point) enjoyable. My learning curve is definitely going straight
up. As it should be for a 'beginner'. Who knows, eventually my days working
with Access all these years could be over and replaced with webapps.

Thoughts on the no data fill?

Hold off on any further work on this. The boss came by and stopped to chat.
I asked him as a user if he searched would he want to see all the rows or
page thru the results? He said he would prefer to see all the search rows.
So I asked several other users, who iterated the same. The reqs indicated
search and paging. So I removed all the ViewState, allowpaging = false,
allowsorting = false, ran it, it all worked, all the rows appeared for the
search. Showed several users and they liked it better. So much for the reqs
(and the scope creep). Have a feeling I will be reviewing this more times
then what was scheduled and changing it as I go. Showing all the searched
rows in this gridview should be okay as the overall gridview should not be
exceeding several hundred rows that would be searched against.

I would like to thank you for your patience and diligence on working thru
this issue.

.... John
 
Ad

Advertisements

J

JohnE

Mark Rae said:
Thoughts on the no data fill?

It looks like ViewState["SearchExpression"] isn't being populated.

Put a breakpoint on the if (!IsPostBack) and check that it's jumping into
the else {...} section...

Just out of curiosity I replaced all the ViewState back as it was and the
breakpoint. It does get into the 'else' part and the txtSearch.text
recognized what was typed in the search field. As I step thru, it goes all
the way thru without error. The dv.RowFilter now indicates "ChangeRequest
like 'eco'". Still nothing showing and the counter field reports back 0
(zero). Puzzling.
.... john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top