473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

GataGrid Command Link Problem

I have a DataGrid where all the columns can be clicked to sort by that column.

A very strange thing is happening: When the page is first displayed with my
default sort order, a click on one of the ButtonColumn LinkButtons works just
fine.

But if I click on one of the column headings and now my control has been
sorted, if I click on one of the ButtonColumn LinkButtons, I get the wrong
row of data back in the "DataGridCommandEventArgs" parameter to DG_Command().
I get the data from some other row.

Any idea why this might be happening?

Alex
Jun 13 '06 #1
7 1100
Hi Alex,

Thank you for your post.

Based on my understanding, the question is:
1) You are using DataGrid in ASP.NET 1.1 and it's bound to some data
source;
2) It's set to sortable and you're using the SortCommand to sort it based
on the clicked column's SortExpression, and rebind it after sort.
3) You have some ButtonColumn which are associated with the row index in
the CommandArgument
4) After sort, the ButtonColumn's associate row index is not correct
If I've misunderstood anything, please feel free to post here.

I think the most possible cause of this problem would be: when you rebind
the DataGrid after sorting, the ButtonColumn's associated row index might
not be updated.

If this is not the case or you need more help on this, would you mind
posting some code here so that we can work on it more closely? Thanks.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 13 '06 #2
Walter -

I think you have this exactly right - Except that I'm working in ASP.NET
2.0. As to Source code, well, I guess I can give you a stripped down version
that's simpler than the real thing and maybe that'll still help you help me!

SAMPLE CODE:
(See comments that start with "//**" below...

protected void Page_Load(object sender, EventArgs e)
{
LoadIssuesDG(m_FilterName, m_SearchStr, m_SortBy);
}

protected void LoadIssuesDG(string FilterName, string SearchStr, string
SortBy)
{
string SQL = BuildQuery(FilterName, SearchStr, SortBy);

OleDbDataReader DR = DB.DoParamSQLGetDataReader(SQL);
if (DR != null)
{
IssuesDG.DataSource = DR;
IssuesDG.DataBind();
}
else
{
ListDescripLB.Text += "<br/>NO ISSUES RETURNED FOR THIS SEARCH.";
}
}
protected void IssuesDG_Sort(Object sender, DataGridSortCommandEventArgs
e)
{
string SortExpression = e.SortExpression.ToString();
LoadIssuesDG(m_FilterName, m_SearchStr, SortExpression);
}

protected void IssuesDG_Command(Object sender, DataGridCommandEventArgs e)
{
string ID = e.Item.Cells[0].Text;
switch (((LinkButton)e.CommandSource).CommandName)
{
case "Edit":
EditItem(ID); //** BEFORE SORT, WORKS FINE. AFTER SORT, ID
IS INCORRECT!
break;

case "View":
ViewItem(ID); //** BEFORE SORT, WORKS FINE. AFTER SORT, ID
IS INCORRECT!
break;

default:
// Do nothing.
break;

}
}

THANKS SO MUCH FOR YOUR HELP!

Alex

"Walter Wang [MSFT]" wrote:
Hi Alex,

Thank you for your post.

Based on my understanding, the question is:
1) You are using DataGrid in ASP.NET 1.1 and it's bound to some data
source;
2) It's set to sortable and you're using the SortCommand to sort it based
on the clicked column's SortExpression, and rebind it after sort.
3) You have some ButtonColumn which are associated with the row index in
the CommandArgument
4) After sort, the ButtonColumn's associate row index is not correct
If I've misunderstood anything, please feel free to post here.

I think the most possible cause of this problem would be: when you rebind
the DataGrid after sorting, the ButtonColumn's associated row index might
not be updated.

If this is not the case or you need more help on this, would you mind
posting some code here so that we can work on it more closely? Thanks.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 13 '06 #3
Hi Alex,

Thank you for your update.

The control's event is fired after the Page_Load event, I saw that you are
always reloading the DataGrid in Page_Load event with following code:

LoadIssuesDG(m_FilterName, m_SearchStr, m_SortBy);

That's fine as long as the m_SortBy variable is persisted between
postbacks, i.e., it's reflected to the current sorted column's key, how did
you persist it?

Normally it will be persisted as follows:

public string SortKey
{
set { ViewState["A"] = value;}
get { return ViewState["A"] as string; }
}

Then in the LoadIssuesDG function, it will be updated:

SortKey = SortBy;

In Page_Load, it will be:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SortKey = "CustomerID";
}
LoadIssuesDG(SortKey);
}
Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 14 '06 #4
Hi Alex,

I am interested in this issue. Would you mind letting me know the result of
the suggestions? If you need further assistance, feel free to let me know.
I will be more than happy to be of assistance.

Have a great day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 16 '06 #5
Wlater -

Hi. Thanks for checking back with me about this. Unfortunately, I have NOT
solved it and it's driving me nuts! I read your advice about persisting the
SortKey in the ViewState but, truth is, I don't think I need to do that and
here's why:

The SortKey is presented to me when the user clicks the header of the column
they want to Sort by. I understand that if I had some other form of
Post-Back, I'd have to persist this value, but for this example, I've checked
and I alsways have a valid Sort Experession. Further, and I think more
importantly, the sorting is obviously happening properly as the textual rows
that I can SEE have, in fact, been sorted appropriately. But what's so
strange is that even though the text rows are sorted, the Link Columns still
point back to the rows as they were originally sorted!

I'm wondering if maybe this is because in the PostBack to Sort scenario, I'm
calling my LoadDG(SortExpression) function twice: the first time in the
PageLoad and then againimmediately thereafter in the DG_Sort() handler, with
the proper new SortOrder. I don't WANT to be doing this but I've noticed that
if I don't populate and data-bind my DataGrid in PageLoad() and I wait 'til
the DG_Sort does it, for some reason the DataGrid doesn't display at all.

So first, is it possible that the row Link Buttons are getting screwed up
because I'm data binding my DataGrid twice in one page loading?

And Second, why do I have to always populate my DataGrid in PageLoad if I
*know* I'm going to be re-Populating it with a defferent sort order in the
DG_Sort handler?

Alex
"Walter Wang [MSFT]" wrote:
Hi Alex,

I am interested in this issue. Would you mind letting me know the result of
the suggestions? If you need further assistance, feel free to let me know.
I will be more than happy to be of assistance.

Have a great day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 17 '06 #6
Well, I've finally figured this out and it's a little NUTS! What was
happening was that the list was displayed properly after Sort and all that
BUT, when the user clicked a CommanLink on one of the rows, upon post-back,
apparently, the control needs the data to have been populated again in the
same order so that the right row can be passed along to the event handler for
the link.

Thanks for your help.

Alex

"Alex Maghen" wrote:
Wlater -

Hi. Thanks for checking back with me about this. Unfortunately, I have NOT
solved it and it's driving me nuts! I read your advice about persisting the
SortKey in the ViewState but, truth is, I don't think I need to do that and
here's why:

The SortKey is presented to me when the user clicks the header of the column
they want to Sort by. I understand that if I had some other form of
Post-Back, I'd have to persist this value, but for this example, I've checked
and I alsways have a valid Sort Experession. Further, and I think more
importantly, the sorting is obviously happening properly as the textual rows
that I can SEE have, in fact, been sorted appropriately. But what's so
strange is that even though the text rows are sorted, the Link Columns still
point back to the rows as they were originally sorted!

I'm wondering if maybe this is because in the PostBack to Sort scenario, I'm
calling my LoadDG(SortExpression) function twice: the first time in the
PageLoad and then againimmediately thereafter in the DG_Sort() handler, with
the proper new SortOrder. I don't WANT to be doing this but I've noticed that
if I don't populate and data-bind my DataGrid in PageLoad() and I wait 'til
the DG_Sort does it, for some reason the DataGrid doesn't display at all.

So first, is it possible that the row Link Buttons are getting screwed up
because I'm data binding my DataGrid twice in one page loading?

And Second, why do I have to always populate my DataGrid in PageLoad if I
*know* I'm going to be re-Populating it with a defferent sort order in the
DG_Sort handler?

Alex
"Walter Wang [MSFT]" wrote:
Hi Alex,

I am interested in this issue. Would you mind letting me know the result of
the suggestions? If you need further assistance, feel free to let me know.
I will be more than happy to be of assistance.

Have a great day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 18 '06 #7
Hi Alex,

Thank you for your update and I'm glad the problem is solved.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 19 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Rilling | last post by:
I have an interesting problem with a datagrid. It is the standard chicken-and-the-egg problem. I have this page with two datagrids. It essentially defines a parent-child relationship. The...
4
by: Bit byte | last post by:
I have a project that I normally build (without problems) from the DevStudio IDE. However, I have embarked on automating all my builds (this test project being one of several). The project...
3
by: andrew | last post by:
Hello, I have spend the past day trying to figure out how to do this. PROBLEM: My instructor wants us to use a shapes library that horstmann wrote. An example would be: /////////////////////////...
4
by: vedrandekovic | last post by:
Hi, I have already install Microsoft visual studio .NET 2003 and MinGw, when I try to build a extension: python my_extension_setup.py build ( or install ) , I get an error: LINK : fatal...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.