datagrid sort custom control question

Discussion in 'ASP .Net Building Controls' started by dave, Feb 3, 2004.

  1. dave

    dave Guest

    I am building a custom scrollable grid control. I have created buttons in the header that postback and provide to me the column that was clicked on. In most datagrid situations it is up to the programmer to now resort the datasource (dataview) and re-order it before rebinding to the grid control

    I am somewhat confused. After a postback my aspx page does not rebind the datagrid with the datasource and it goes right to the custom datagrid control _load procedure. From here it recreates the datagrid. When/where do I integrate the sorting?
    It would appear that I would have to capture the column heading click in the aspx page and rebind the grid. Please provide some guidance with this project

    dave, Feb 3, 2004
    1. Advertisements

  2. Hi dave,

    Thank you for posting in the community!

    Based on my understanding, you use button server control in column's
    headertemplate, and you want to sort this column when click this button.

    Yes, just as you said, you should capture the button click event and sort
    the data source, then rebind it to the datagrid.

    Sample code like this:
    private void Page_Load(object sender, System.EventArgs e)

    public void getsource()
    SqlDataAdapter adapter=new SqlDataAdapter("select * from
    ds=new DataSet();

    public void databind()

    private void DataGrid1_ItemCreated(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    DataGridItem dgi=e.Item;
    foreach(Control c in dgi.Cells[0].Controls)
    if(c is System.Web.UI.WebControls.Button)
    Button bt=(Button)c;
    bt.Click+=new EventHandler(bt_Click);

    private void bt_Click(object sender, EventArgs e)
    dv.Sort="job_id DESC";

    I use Sql server's default "jobs" table in "pubs" database to bind to

    Please apply my suggestion above and let me know if it helps resolve your

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.
    Have a nice day!!

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! -
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Feb 3, 2004
    1. Advertisements

  3. dave

    dave Guest

    My event handler for the sorting is within the custom control, however when resorting i will have to databind again the datagrid which is within the page aspx logic.

    If a user clicks on a header i can capture that event in the custom control but i need to execute the databind again
    Dim dv As DataView = dt.DefaultView
    dv.Sort = myDataGrid1.SortExpression
    With myDataGrid1
    .DataSource = dv
    End With

    That code is within my aspx page. My problem is, how do i generate the rebind from within the custom control?
    dave, Feb 3, 2004
  4. dave

    dave Guest

    Well i have the problem identified
    I can capture the click event in the header of the grid ok. The problem is after a user clicks on the header control (in the datagrid) code in the page (the rebind) is performed before i capture the event of the click (to sort) in the header

    After the grid is rebound to the sorted grid the click event in the control is executed but at this point the datasource has already been sorted

    How can i ensure that i process the click event of the control header button before the aspx rebinds and sorts the datasource

    Currently i capture the click event in the control in the controls load event as follows

    Dim s As String = context.Request.Form("__EVENTTARGET"
    If InStr(s, "LinkID") > 0 The
    Call DataGrid_Sort(s
    End I
    dave, Feb 4, 2004
  5. dave

    dave Guest

    The answer to this problem was putting my routine to capture the post back in the myDataGrid_DataBinding event.
    dave, Feb 4, 2004
  6. Hi dave,

    Thanks very much for your feedback.

    I see your concern, you used customized datagrid control, and you want to
    handle the event in the control level not the page level.(I also see that
    you prefer to use, so I will write my sample project in for

    I think the implement based on your design of your web application.

    If you want to handle the header "sort button" click event in your
    customized datagrid control, you can add click event handler for the
    I suppose you only use the DataView as the datasource, then in the event
    handler, you can get the dataview from the datasource.(cast the datasource
    to dataview explicitly)

    So you can set the Sort string in your control, then you can re-bind your

    Sample code like this:

    1).Customized datagrid control's code:
    <ToolboxData("<{0}:Customdatagrid runat=server></{0}:Customdatagrid>")>
    Public Class Customdatagrid
    Inherits System.Web.UI.WebControls.DataGrid

    Property SortExpression() As String
    Return CType(Me.ViewState("SortExpression"), String)
    End Get
    Set(ByVal Value As String)
    Me.ViewState("SortExpression") = Value
    End Set
    End Property

    Protected Overrides Sub OnItemCreated(ByVal e As
    If e.Item.ItemType = ListItemType.Header Then
    Dim dgi As DataGridItem = e.Item

    Dim bt As Button = New Button
    bt.ID = "bt"
    bt.Text = "SortButton"
    AddHandler bt.Click, AddressOf bt_Click

    End If
    End Sub

    Private Sub bt_Click(ByVal sender As Object, ByVal e As
    If (Not Me.DataSource Is Nothing) And (TypeOf (Me.DataSource) Is
    System.Data.DataView) Then
    Dim dv As DataView = CType(Me.DataSource, DataView)
    dv.Sort = Me.SortExpression
    End If
    End Sub
    End Class

    2). The Web Page's code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Customdatagrid1.SortExpression = "job_id DESC"
    Customdatagrid1.DataSource = getsource()
    If (Not IsPostBack) Then
    End If
    End Sub

    Private Function getsource() As DataView
    Dim adapter As SqlDataAdapter = New SqlDataAdapter("select * from
    jobs", "server=localhost;database=pubs;uid=sa;pwd=")
    Dim ds As DataSet = New DataSet
    Dim dv As DataView = ds.Tables(0).DefaultView
    Return dv
    End Function

    More: I recommand you expose your header button's click event as its parent
    control's(your customized datagrid) event, then you can handle this in the
    page level.
    This is called Bubble child control event to parent control.

    For more information about how to Bubble event in .Net, please refer to:

    If you still have anything unclear, please feel free to tell me, I will
    help you.
    Have a nice day!!

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! -
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Feb 4, 2004
  7. Hi dave,

    Thanks very much for your feedback.

    I am glad you got what you want. If you have further concern, please feel
    free to post, I will help you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! -
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Feb 9, 2004
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson
    Scott Allen
    Apr 5, 2005
  2. J R M
    Jun 1, 2006
  3. Matt Sokol
    Theo Tillotson
    Aug 7, 2003
  4. SK

    Sort in custom datagrid control

    SK, Feb 17, 2005, in forum: ASP .Net Datagrid Control
  5. Navin
    Ken Schaefer
    Sep 9, 2003

Share This Page