Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the best way to return column info 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I want to return what column the user clicked in. What is the best way to do this?

I want to show an up arrow in the heading, or a down arrow, depending on the sort method used.
 
Someone has to have done this. User clicks on a heading and sorts the datagrid by a column, and a marker appears next to the column header showing the sort. Just like Access, Just like Outlook. Ya know.

My users want everything. This should not be so hard.
 
DataGrid.Columns[Index].HeaderImageUrl...

Have you tried using the above to add an image to the DataGrid Column Header, in yourevent which sorts the datagrid?

Rhys
 
No, because I don't know how to return the column that the the user clicked on...

That's my problem. What stinking column did he click on. Sorry, but I look in all the documentation (that stinks, and is written for C programmers).

Getting frustrated with this grid. There must be some good documentation somewhere. Read 4guys from rolla. I never liked MS's descriptions. They are written by geeks. :)
 
I take it then, that you're not actually 'SORTING' the datagrid yet either?

Rhys

Will code for food...
 
Yes. The sorting works fine, Rhys. I just can't figure out what column I'm clicking on. Makes no sense, huh? :)
 
Are these Bound Columns?

Are they Auto Generated?

Sorry about asking the questions, I'm tring to work out how to get a pointer to the Index of the column you're sorting by...

Rhys

Will code for food...
 
No, they are not auto generated. The first 4 columns are:
Edit button, Delete button, Show details button, sequence number hidden. Then it starts with CustNum, CustName1, Address1,....

Any and all help would be much needed. Thank you so much!
 
What is the datasource for the datagrid?

How are you identifying the column to sort by? Can I see this bit of your code, (the sort by function/event)?

Rhys

Will code for food...
 
Ok, Here is the DataGrid's HTML code first:

<asp:datagrid id=&quot;dgAgwayCust&quot; style=&quot;Z-INDEX: 108; LEFT: 8px; POSITION: absolute; TOP: 48px&quot;
runat=&quot;server&quot; Height=&quot;286px&quot; OnItemCommand=&quot;dgAgwayCustButton_Click&quot; PageSize=&quot;6&quot; DataKeyField=&quot;SeqNo&quot; BackColor=&quot;White&quot; BorderStyle=&quot;None&quot; BorderWidth=&quot;1px&quot; BorderColor=&quot;#999999&quot;
OnSelectedIndexChanged=&quot;dgAgwayCust_Select&quot; AutoGenerateColumns=&quot;False&quot; CellPadding=&quot;3&quot; PagerStyle-Mode=&quot;NextPrev&quot; AllowPaging=&quot;True&quot; OnPageIndexChanged=&quot;dgAgwayCust_Page&quot; GridLines=&quot;Vertical&quot;
Font-Names=&quot;Verdana&quot; Font-Size=&quot;8pt&quot; AllowSorting=&quot;True&quot; OnSortCommand=&quot;dgAgwayCust_SortEventHandler&quot;>
<SelectedItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana&quot; Font-Bold=&quot;True&quot; Wrap=&quot;False&quot; ForeColor=&quot;White&quot;
BackColor=&quot;#008A8C&quot;></SelectedItemStyle>
<EditItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana&quot; Wrap=&quot;False&quot;></EditItemStyle>
<AlternatingItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana,Arial,Helvetica,sans-serif&quot; Wrap=&quot;False&quot; BackColor=&quot;Gainsboro&quot;></AlternatingItemStyle>
<ItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana,Arial,Helvetica,sans-serif&quot; Wrap=&quot;False&quot; ForeColor=&quot;Black&quot;
BackColor=&quot;#EEEEEE&quot;></ItemStyle>
<HeaderStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana,Arial,Helvetica,sans-serif&quot; Font-Bold=&quot;True&quot;
Wrap=&quot;False&quot; HorizontalAlign=&quot;Center&quot; ForeColor=&quot;White&quot; BackColor=&quot;#000084&quot;></HeaderStyle>
<FooterStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana,Arial,Helvetica,sans-serif&quot; HorizontalAlign=&quot;Center&quot;
ForeColor=&quot;Black&quot; BackColor=&quot;#CCCCCC&quot;></FooterStyle>
<Columns>
<asp:ButtonColumn Text=&quot;Edit&quot; CommandName=&quot;Edit&quot;>
<ItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot;></ItemStyle>
</asp:ButtonColumn>
<asp:ButtonColumn Text=&quot;Delete&quot; CommandName=&quot;Delete&quot;>
<ItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot;></ItemStyle>
</asp:ButtonColumn>
<asp:ButtonColumn Text=&quot;Show details&quot; CommandName=&quot;Select&quot;>
<ItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot;></ItemStyle>
</asp:ButtonColumn>
<asp:BoundColumn Visible=&quot;False&quot; DataField=&quot;SeqNo&quot;>
<ItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana&quot;></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustNo&quot; SortExpression=&quot;CustNo&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Customer Nbr&quot;>
<HeaderStyle Width=&quot;80px&quot;></HeaderStyle>
<ItemStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana&quot;></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustName1&quot; SortExpression=&quot;CustName1&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Customer Name&quot;>
<HeaderStyle Width=&quot;250px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustAddr1&quot; SortExpression=&quot;CustAddr1&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Address&quot;>
<HeaderStyle Width=&quot;250px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustCity&quot; SortExpression=&quot;CustCity&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;City&quot;>
<HeaderStyle Width=&quot;130px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustST&quot; SortExpression=&quot;CustST&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;ST&quot;>
<HeaderStyle Width=&quot;30px&quot;></HeaderStyle>
<ItemStyle HorizontalAlign=&quot;Center&quot;></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustZip&quot; SortExpression=&quot;CustZip&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Zip&quot;>
<HeaderStyle Width=&quot;50px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustPhone&quot; SortExpression=&quot;CustPhone&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Phone&quot;>
<HeaderStyle Width=&quot;150px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustFax&quot; SortExpression=&quot;CustFax&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Fax&quot;>
<HeaderStyle Width=&quot;150px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;SalesNumber&quot; SortExpression=&quot;SalesNumber&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Sls&quot;>
<HeaderStyle Width=&quot;50px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn Visible=&quot;False&quot; DataField=&quot;SeqNo&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;SeqNo&quot;></asp:BoundColumn>
</Columns>
<PagerStyle Font-Size=&quot;8pt&quot; Font-Names=&quot;Verdana,Arial,Helvetica,sans-serif&quot; HorizontalAlign=&quot;Center&quot;
ForeColor=&quot;Black&quot; BackColor=&quot;#999999&quot; Mode=&quot;NumericPages&quot;></PagerStyle>
</asp:datagrid>


Now here are some functions:

Sub dgAgwayCust_Page(ByVal Sender As Object, ByVal E As DataGridPageChangedEventArgs)
' display a new page of data
dgAgwayCust.EditItemIndex = -1
dgAgwayCust.CurrentPageIndex = E.NewPageIndex
BindCustGrid(strCustGridSort)
BindCallGrid()
End Sub

Sub dgAgwayCust_SortEventHandler(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
strCustGridSort = e.SortExpression & &quot; ASC&quot;
BindCustGrid(strCustGridSort)


'Dim dgHeader As DataGridItem
'dgHeader = e.CommandSource


'TextBox1.Text = dgAgwayCust.Columns(dgAgwayCust.SelectedIndex).HeaderText
'TextBox1.Text = dgAgwayCust.SelectedItemStyle.BackColor.ToString
'dgAgwayCust.SelectedItemStyle.
End Sub


Disregard the remarks. I was trying to find the header.
 
So, what is e.SortExpression come out as, and what's the source of the datagrid, (DataTable, DataView...)

Rhys

Will code for food...
 
'Define the connection object
Dim objConnection As New SqlConnection(strConnection)
Dim objDataAdapter As New SqlDataAdapter(strSQLCommand, objConnection)

'Define the DataSet
Dim dsCust As New DataSet

'Fill the DataSet with the database data
objDataAdapter.Fill(dsCust)

'Not needed
'objConnection.Close()

'Bind the DataGrid to the DataSet
dgAgwayCust.DataSource = dsCust
dgAgwayCust.DataBind()
dgAgwayCust.DataKeyField = &quot;SeqNo&quot;
 
What I'm wondering is this...
You seem to be able to relate e.SortExpression back to your SQL to fill your DataTable, and you can iterate through the columns of a DataTable. Are the DataTable columns all bound to DataGrid columns, and if so are they bound sequentially?
Basically, if you ignore your Button columns, is the dataGrid the same as the DataTable?

Rhys

Will code for food...
 
Yes. You are correct. There should be something like this line;

myGrid.Columns(myGrid.CurrentColumn).Header.Text=&quot;new heading&quot;
Don't you agree?
 
Trying to think 'outside of the box', you can iterate through the columns collection of DataTable's. If you put a breakpoint in...

strCustGridSort = e.SortExpression & &quot; ASC&quot;
BindCustGrid(strCustGridSort)-->HERE

Can you use the command window to find what e.SortExpression equals.

If, as I suspect, it's the same as the equivalent column name in the DataTable, can't you iterate through the columns in the source datatable, get the column index of the column which is the datasource for the column your sorting by, by matching the column name against the value of e.SortExpression, add to it the number of ButtonColumns preceding your Bound columns in the DataGrid and ssubsequently get the index of the bound column you're interested in?

Sounds long winded, and as I'm now at home I can't play with this for you on my .net machine to get the code unfortunately, but something along these lines may get the info you want, as when you get the index of your DataGrid column you're set up to be able to use DataGrid.Columns[Index].HeaderImageUrl...




Rhys

Will code for food...
 
It is the column name id, which I set to the name of the column in SQL, i.e. field name.
 
If you can wait till tomorrow, I'll get some syntax together for you, but I have to get off now. If you give it a go, let me know how you get on, I'm quite interested in this, it might be useful...

Rhys

Will code for food...
 
I'm out of here in an hour anyway. Yes, I'd love some help. Thanks Rhys....
 
jn03, The problems not sorting the datagrid but providing an up or down arrow image in the header of the column the grids sorted by, with the arroe in the appropriate direction of course...

Rhys

Will code for food...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top