GridView Sorting Problem

G

GaryDean

I have a GridView datasourced by a SQLDataSource with Enable Sorting and
Enable Selection turned on. However I don't like the headings and all of
the columns that come from the SQLDataSource in in the RowDataBound event I
do...
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[billmethod].Visible = false;
e.Row.Cells[dateissued].Visible = false;
e.Row.Cells[receiptamt].Visible = false;
e.Row.Cells[cbmark].Text = "Mark";
e.Row.Cells[viewinvoice].Text = "View Doc";
e.Row.Cells[invoicenumber].Text = "Invoice #";
e.Row.Cells[billtoorganization].Text = "Bill To Company";
e.Row.Cells[createdate].Text = "Created";
e.Row.Cells[billcycle].Text = "Pay Type";
e.Row.Cells[status].Text = "Status";
e.Row.Cells[invamt].Text = "Bill Amt";
e.Row.Cells[amtreceived].Text = "Amt Rec";
e.Row.Cells[orgtype].Text = "Aging";


}

This has apparently nuked my sorting feature because my custom headings are
not underlined and clicking on them does not sort. They are born dead. Now
if I hit the select button on any row, the grid shows the headings from SQL
and the sort feature is restored. It stays restored until the next time
RowDataBound is triggered.

I want to have custom headings and the sort capability. Is this possible
without changing my sql?
Thanks,
Gary
 
A

Allen Chen [MSFT]

Hi Gary,

My Name is Allen Chen. It's my pleasure to work with you on this issue.

From your description, you want to use the auto generated columns of the
GridView and to replace the default header LinkButton with your own custom
one. Is my understanding correct?

If so please try the following code in the RowCreated event hanlder of the
GridView control:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{ Button b=new Button();
b.Text = "Custom Sort Button!";
b.CommandName="Sort";
b.CommandArgument="theID";//the field you want to sort
e.Row.Cells[0].Controls.Clear();
e.Row.Cells[0].Controls.Add(b);
}
}

Here a new button will replace the default LinkButton on the header of the
first column.

The key point is to set the CommandName and the CommandArgument. The
CommandName is mandatory to be "Sort". It means you want to use this button
to sort GridView. The CommandArgument represents the field you want to
sort. You need to replace it with the real one in your case.

Please have a try and let me know if it works. If you need further
assistance please don't hesitate to let me know.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

GaryDean

Allen:
Thanks for your answer.

Actually my goal was not to replace the whole button although I tested your
code and it did work. My goal was just to put up a more friendly text
header than the one returned by SQL i.e. "Invoice #" instead of
"invoicenumber". But learning from your example prompted me to use the
following two lines of code in the RowDataBound event and this also worked:

LinkButton b =
(LinkButton)e.Row.Cells[invoicenumber].Controls[0];
b.Text = "Friendly Heading";
So, problem solved.
Thanks,
Gary

Allen Chen said:
Hi Gary,

My Name is Allen Chen. It's my pleasure to work with you on this issue.

From your description, you want to use the auto generated columns of the
GridView and to replace the default header LinkButton with your own custom
one. Is my understanding correct?

If so please try the following code in the RowCreated event hanlder of the
GridView control:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{ Button b=new Button();
b.Text = "Custom Sort Button!";
b.CommandName="Sort";
b.CommandArgument="theID";//the field you want to sort
e.Row.Cells[0].Controls.Clear();
e.Row.Cells[0].Controls.Add(b);
}
}

Here a new button will replace the default LinkButton on the header of the
first column.

The key point is to set the CommandName and the CommandArgument. The
CommandName is mandatory to be "Sort". It means you want to use this
button
to sort GridView. The CommandArgument represents the field you want to
sort. You need to replace it with the real one in your case.

Please have a try and let me know if it works. If you need further
assistance please don't hesitate to let me know.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
A

Allen Chen [MSFT]

Hi Gary,

Glad to know this issue has been solved. Have a nice day!

Regards,
Allen Chen
Microsoft Online Support
 

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

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top