BoundColumn

R

rn5a

A DataGrid has a BoundColumn which changes to a TextBox when the
DataGrid is in the editable mode. The rest of the columns in the
DataGrid are TemplateColumns. There's an EditCommandColumn as well.
All the columns including the BoundColumn but excluding the
EditCommandColumn can be sorted. The column sorted is also accompanied
by an image to indicate whether a column has been sorted ascendingly
or descendingly. The image gets rendered when the ItemDataBound event
of the DataGrid fires. This is the DataGrid:

<asp:DataGrid ID="dgCart"
OnEditCommand="EditCart"...OnItemDataBound="BindCart" runat="server">

<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkPID" CommandArgument="PID" Text="ID"
runat="server"/>&nbsp;<img id="imgPID" src="Up.gif" visible="false"
runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPID" Text='<%# Container.DataItem("PID") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkPName" CommandArgument="PName" Text="PRODUCT"
runat="server"/>&nbsp;<img id="imgPName" src="Up.gif" visible="false"
runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPName" Text='<%# Container.DataItem("PName") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>

<%-- a few more TemplateColumns as shown above come here --%>

<asp:BoundColumn DataField="Qty" HeaderText="QTY" SortExpression="Qty"/
<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="EDIT" UpdateText="UPDATE"/>

</asp:DataGrid>

The problem is when the DataGrid is sorted by the QTY column
irrespective of whether the DataGrid is in the editable mode or not, I
can't seem to find a way to render the image along with the HeaderText
*QTY* since it's a BoundColumn.

Is there any way by which I can make the HeaderText display the image
when the DataGrid is sorted by the QTY column?

Replacing the BoundColumn with a TemplateColumn, adding a TextBox in
the ItemTemplate of the TemplateColumn & making it visible/invisible
as & when the EDIT button is clicked could be one option but that I
guess would entail a lot of additional code to make the TextBox
corresponding to the row in which the EDIT button is clicked visible.
 
G

Guest

Hi,
Please find below some that i use in situations like your's, the code
changes the header to add an image that represent the current sort order

#Region "Data Grid Sorting"
Private Sub grdResult_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
grdResult.SortCommand

If ViewState(e.SortExpression + "SortDirection") Is Nothing Then
ViewState(e.SortExpression + "SortDirection") = "ASC"
End If
ChangeHeaderImage(e.SortExpression, ViewState(e.SortExpression +
"SortDirection").ToString())
BindGrid(e.SortExpression + " " + ViewState(e.SortExpression +
"SortDirection"))
SwapSortDirection(e.SortExpression + "SortDirection")

End Sub
Sub ChangeHeaderImage(ByVal sortBy As String, ByVal dir As String)

Dim im As String
If dir = "ASC" Then
im = " <img border=0 src=""../images/up.gif"" > "
Else
im = " <img border=0 src=""../images/dn.gif"" > "
End If
Dim col As DataGridColumn
For Each col In grdResult.Columns
If col.SortExpression = sortBy Then
Dim text As String = col.HeaderText
Dim pos As Integer = col.HeaderText.IndexOf(" ")
If pos > -1 Then
text = col.HeaderText.Substring(0, pos)
End If
col.HeaderText = String.Concat(text, im)
Else
Dim text As String = col.HeaderText
Dim pos As Integer = col.HeaderText.IndexOf(" ")
If (pos > -1) Then
col.HeaderText = col.HeaderText.Substring(0, pos)
End If
End If
Next
End Sub

Sub ClearHeaderImages()

Dim col As DataGridColumn
For Each col In grdResult.Columns
Dim text As String = col.HeaderText
Dim pos As Integer = col.HeaderText.IndexOf(" ")
If (pos > -1) Then
col.HeaderText = col.HeaderText.Substring(0, pos)
End If
Next
End Sub
Sub SwapSortDirection(ByVal viewStateKey As String)
If ViewState(viewStateKey).ToString() = "ASC" Then
ViewState(viewStateKey) = "DESC"
Else
ViewState(viewStateKey) = "ASC"
End If
End Sub
#End Region
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top